相对于变量 ActionResult 的 MVC Url

发布于 2024-10-03 22:25:23 字数 477 浏览 0 评论 0原文

对 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿与i 2024-10-10 22:25:23

你可以使用这样的代码(一个javascript技巧):

<a href="" onclick="window.location = window.location + @item.ID">@item.Name</a>

或者,如果你想使用 href 属性,你可以使用这样的代码:(

<%=Html.ActionLink(item.Name, "Facilities", ViewData("ActionName"), new {id = item.ID}) %>

在这种情况下,你必须在控制器中指定 ViewData("ActionName") )。

You can use such code (a javascript trick):

<a href="" onclick="window.location = window.location + @item.ID">@item.Name</a>

Or, if you want to use href property, you can use such code:

<%=Html.ActionLink(item.Name, "Facilities", ViewData("ActionName"), new {id = item.ID}) %>

(In that case you have to specify ViewData("ActionName") in controller).

死开点丶别碍眼 2024-10-10 22:25:23

我建议您更改它,以便只有一个操作,但它需要一个参数,并且根据参数将不同的列表推送到视图(也许使用 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文