HTML.ActionLink方法

假设我有一堂课

public class ItemController:Controller { public ActionResult Login(int id) { return View("Hi", id); } } 

在没有位于ItemController所在的Item文件夹的页面上,我想创build一个指向Login方法的链接。 那么我应该使用哪个Html.ActionLink方法,以及我应该传递哪些参数?

具体来说,我正在寻找替代的方法

 Html.ActionLink(article.Title, new { controller = "Articles", action = "Details", id = article.ArticleID }) 

已经在最近的ASP.NET MVC化身中退役了。

我想你想要的是这样的:

ASP.NET MVC1

 Html.ActionLink(article.Title, "Login", // <-- Controller Name. "Item", // <-- ActionMethod new { id = article.ArticleID }, // <-- Route arguments. null // <-- htmlArguments .. which are none. You need this value // otherwise you call the WRONG method ... // (refer to comments, below). ) 

这使用以下方法ActionLink签名:

 public static string ActionLink(this HtmlHelper htmlHelper, string linkText, string controllerName, string actionName, object values, object htmlAttributes) 

ASP.NET MVC2

两个参数已经转换

 Html.ActionLink(article.Title, "Item", // <-- ActionMethod "Login", // <-- Controller Name. new { id = article.ArticleID }, // <-- Route arguments. null // <-- htmlArguments .. which are none. You need this value // otherwise you call the WRONG method ... // (refer to comments, below). ) 

这使用以下方法ActionLink签名:

 public static string ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object values, object htmlAttributes) 

ASP.NET MVC3 +

参数的顺序与MVC2相同,但是不再需要id值:

 Html.ActionLink(article.Title, "Item", // <-- ActionMethod "Login", // <-- Controller Name. new { article.ArticleID }, // <-- Route arguments. null // <-- htmlArguments .. which are none. You need this value // otherwise you call the WRONG method ... // (refer to comments, below). ) 

这避免了将任何路由逻辑硬编码到链路中。

  <a href="/Item/Login/5">Title</a> 

这会给你下面的html输出,假设:

  1. article.Title = "Title"
  2. article.ArticleID = 5
  3. 您仍然定义了以下路由

。 。

 routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } // Parameter defaults ); 

我想补充一下Joseph Kingry的回答 。 他提供了解决scheme,但起初我无法使其工作,并得到了像Adhip Gupta一样的结果。 然后我意识到路线必须首先存在,参数需要与路线完全匹配。 所以我有一个id,然后是我的路线的文本参数,这也需要包括在内。

 Html.ActionLink(article.Title, "Login", "Item", new { id = article.ArticleID, title = article.Title }, null) 

您可能需要查看RouteLink()方法。您可以通过字典指定所有内容(链接文本和路由名称除外)。

我认为约瑟夫控制器和行动。 首先来操作然后控制器。 这有点奇怪,但是签名的样子。

只是为了澄清事情,这是作品的版本(适应约瑟夫的例子):

 Html.ActionLink(article.Title, "Login", // <-- ActionMethod "Item", // <-- Controller Name new { id = article.ArticleID }, // <-- Route arguments. null // <-- htmlArguments .. which are none ) 
 Html.ActionLink(article.Title, "Login/" + article.ArticleID, 'Item") 

那这个呢

 <%=Html.ActionLink("Get Involved", "Show", "Home", new { id = "GetInvolved" }, new { @class = "menuitem", id = "menu_getinvolved" } )%> 

如果你想要所有的花式裤子,这里是如何扩展它能够做到这一点:

 @(Html.ActionLink<ArticlesController>(x => x.Details(), article.Title, new { id = article.ArticleID })) 

你将需要把它放在System.Web.Mvc命名空间中:

 public static class MyProjectExtensions { public static MvcHtmlString ActionLink<TController>(this HtmlHelper htmlHelper, Expression<Action<TController>> expression, string linkText) { var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection); var link = new TagBuilder("a"); string actionName = ExpressionHelper.GetExpressionText(expression); string controllerName = typeof(TController).Name.Replace("Controller", ""); link.MergeAttribute("href", urlHelper.Action(actionName, controllerName)); link.SetInnerText(linkText); return new MvcHtmlString(link.ToString()); } public static MvcHtmlString ActionLink<TController, TAction>(this HtmlHelper htmlHelper, Expression<Action<TController, TAction>> expression, string linkText, object routeValues) { var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection); var link = new TagBuilder("a"); string actionName = ExpressionHelper.GetExpressionText(expression); string controllerName = typeof(TController).Name.Replace("Controller", ""); link.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues)); link.SetInnerText(linkText); return new MvcHtmlString(link.ToString()); } public static MvcHtmlString ActionLink<TController>(this HtmlHelper htmlHelper, Expression<Action<TController>> expression, string linkText, object routeValues, object htmlAttributes) where TController : Controller { var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection); var attributes = AnonymousObjectToKeyValue(htmlAttributes); var link = new TagBuilder("a"); string actionName = ExpressionHelper.GetExpressionText(expression); string controllerName = typeof(TController).Name.Replace("Controller", ""); link.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues)); link.MergeAttributes(attributes, true); link.SetInnerText(linkText); return new MvcHtmlString(link.ToString()); } private static Dictionary<string, object> AnonymousObjectToKeyValue(object anonymousObject) { var dictionary = new Dictionary<string, object>(); if (anonymousObject == null) return dictionary; foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(anonymousObject)) { dictionary.Add(propertyDescriptor.Name, propertyDescriptor.GetValue(anonymousObject)); } return dictionary; } } 

这包括Route ValuesHTML Attributes两个重写,同样,所有的视图都需要添加: @using YourProject.Controllers或者你可以将它添加到你的web.config <pages><namespaces>

使用命名参数以获得最佳可读性并避免混淆:

 @Html.ActionLink( linkText: "Click Here", actionName: "Action", controllerName: "Home", routeValues: new { Identity = 2577 }, htmlAttributes: null) 

与MVC5我已经这样做了,它是100%的工作代码….

 @Html.ActionLink(department.Name, "Index", "Employee", new { departmentId = department.DepartmentID }, null) 

你们可以从中得到一个想法…

这种types使用:

@ Html.ActionLink( “的MainPage”, “索引”, “家”)

MainPage:文本的名称索引:Action View Home:HomeController

基本使用ActionLink

 <html> <head> <meta name="viewport" content="width=device-width" /> <title>_Layout</title> <link href="@Url.Content("~/Content/bootsrap.min.css")" rel="stylesheet" type="text/css" /> </head> <body> <div class="container"> <div class="col-md-12"> <button class="btn btn-default" type="submit">@Html.ActionLink("AnaSayfa","Index","Home")</button> <button class="btn btn-default" type="submit">@Html.ActionLink("Hakkımızda", "Hakkimizda", "Home")</button> <button class="btn btn-default" type="submit">@Html.ActionLink("Iletişim", "Iletisim", "Home")</button> </div> @RenderBody() <div class="col-md-12" style="height:200px;background-image:url(/img/footer.jpg)"> </div> </div> </body> </html>