asp.net-mvc 使用 Html.ActionLink 调用不同控制器上的动作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/776781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Html.ActionLink to call action on different controller
提问by kaivalya
I am trying to navigate between controllers using ActionLink. I will tell my problem with an example.
我正在尝试使用ActionLink. 我会用一个例子来说明我的问题。
I am on Index view of Hat controller, and I am trying to use below code to create a link to Details action of Product controller.
我在帽子控制器的索引视图上,我试图使用下面的代码来创建一个链接到产品控制器的详细信息操作。
<%= Html.ActionLink("Details", "Details", "Product", new { id=item.ID }) %>
Instead of creating a link to Details on Product controller, this generates a link to Details action under Hat controller and appends a Length parameter to the end of it:
这不是在 Product 控制器上创建一个到 Details 的链接,而是在 Hat 控制器下生成一个到 Details 操作的链接,并将一个 Length 参数附加到它的末尾:
Hat/Details/9?Length=7
I am not able to use HTML.ActionLinkto switch between controllers because of this problem. I will appreciate if you can point me to what I am doing wrong. Thanks
HTML.ActionLink由于这个问题,我无法在控制器之间切换。如果您能指出我做错了什么,我将不胜感激。谢谢
PS: I am using the default route setting that comes with MVC
PS:我使用的是MVC自带的默认路由设置
routes.MapRoute("Default", "{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" } );
回答by ?a?da? Tekin
What you want is this overload :
你想要的是这个重载:
//linkText, actionName, controllerName, routeValues, htmlAttributes
<%=Html.ActionLink("Details", "Details",
"Product", new {id = item.ID}, null) %>
回答by Stephan Venter
With that parameters you're triggering the wrong overloaded function/method.
使用这些参数,您将触发错误的重载函数/方法。
What worked for me:
什么对我有用:
<%= Html.ActionLink("Details", "Details", "Product", new { id=item.ID }, null) %>
It fires HtmlHelper.ActionLink(string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
它触发 HtmlHelper.ActionLink(string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
I'm using MVC 4.
我正在使用 MVC 4。
Cheerio!
啦啦啦!
回答by Psi-Ed
I would recommend writing these helpers using named parameters for the sake of clarity as follows:
为了清楚起见,我建议使用命名参数编写这些助手,如下所示:
@Html.ActionLink(
linkText: "Details",
actionName: "Details",
controllerName: "Product",
routeValues: new {
id = item.ID
},
htmlAttributes: null
)
回答by James Avery
If you grab the MVC Futures assembly (which I would highly recommend) you can then use a generic when creating the ActionLink and a lambda to construct the route:
如果您获取 MVC Futures 程序集(我强烈推荐),则可以在创建 ActionLink 和 lambda 时使用泛型来构造路由:
<%=Html.ActionLink<Product>(c => c.Action( o.Value ), "Details" ) %>
You can get the futures assembly here: http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471
您可以在此处获取期货程序集:http: //aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471
回答by Craig Stuntz
You're hitting the wrong the overload of ActionLink. Try this instead.
你打错了 ActionLink 的过载。试试这个。
<%= Html.ActionLink("Details", "Details", "Product", new RouteValueDictionary(new { id=item.ID })) %>
回答by sandy barasker
try it it is working fine
试试看它工作正常
<%:Html.ActionLink("Details","Details","Product", new {id=item.dateID },null)%>
回答by George Chondrompilas
An alternative solution would be to use the Urlhelper object to set the hrefattribute of an <a>tag like:
另一种解决方案是使用Urlhelper 对象来设置标签的href属性,<a>如:
<a href="@Url.Action("Details", "Product",new { id=item.ID }) )">Details</a>
回答by AnotherOne
Note that Details is a "View" page under the "Products" folder.
请注意,详细信息是“产品”文件夹下的“查看”页面。
ProductId is the primary key of the table . Here is the line from Index.cshtml
ProductId 是表的主键。这是 Index.cshtml 中的行
@Html.ActionLink("Details", "Details","Products" , new { id=item.ProductId },null)
回答by Amin Saadati
this code worked for me in partial view:
这段代码在部分视图中对我有用:
<a href="/Content/[email protected]">@item.Title</a>

