对 MVC 相当陌生。我的问题是我有一个控制器,它有 3 个不同的 ActionResult,它们根据枚举进行调用。
所有三个操作结果都返回相同的视图,但具有不同的列表作为视图模型。在视图中,用户应该能够单击列表中的项目并根据该项目的 ID 查看详细信息。
例如,Site/Facilities/Libraries 返回图书馆列表,Site/Facilities/Libraries/1 返回详细信息。当您输入完整路径时,此方法工作正常,但在视图本身上,单击
<a href="@item.ID">@item.Name</a>
列表中某个项目上的锚点会返回 Url Site/Facilities/1 而不是 Site/Facilities/Libraries/1。我无法使用 ActionLink,因为要调用的操作是动态的。我知道这可以通过为每种类型创建不同的视图来解决,但我想知道是否还有其他方法?
提前致谢。
Reasonably new to MVC. My problem is that I have a controller that has 3 different ActionResult's which are called depending on an enum.
All three action results return the same view but with different lists as the views model. In the view the user should be able to click on an item in the list and view the details based on the ID of the item.
e.g. Site/Facilities/Libraries returns the list of libraries, Site/Facilities/Libraries/1 returns the details. This works fine when you enter the full path but on the View itself clicking the anchor
<a href="@item.ID">@item.Name</a>
on an item in the list returns the Url Site/Facilities/1 instead of Site/Facilities/Libraries/1. I cant use an ActionLink as the Action to call is dynamic. I know this could be solved by creating a different View for each type but I wondered if there might be another way?
Thanks in advance.
发布评论
评论(2)
你可以使用这样的代码(一个javascript技巧):
或者,如果你想使用 href 属性,你可以使用这样的代码:(
在这种情况下,你必须在控制器中指定 ViewData("ActionName") )。
You can use such code (a javascript trick):
Or, if you want to use href property, you can use such code:
(In that case you have to specify ViewData("ActionName") in controller).
我建议您更改它,以便只有一个操作,但它需要一个参数,并且根据参数将不同的列表推送到视图(也许使用 3 个不同的“辅助函数”)。这至少是我将如何实现你所描述的!
祝你好运!
I would suggest that you change it so that you only have one Action, but it takes an argument instead, and depending on the argument you push different lists to the View (perhaps using 3 different "helper-functions" instead). That's at least how I would implement what you are describing!
Good luck!