超文本标记语言ActionLink方法

假设我有一个类

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

ItemController所在的Item文件夹之外的页面上,我想创建一个指向Login方法的链接。所以我应该使用哪个Html.ActionLink方法,我应该传递什么参数?

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

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

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

791544 次浏览
Html.ActionLink(article.Title, "Login/" + article.ArticleID, 'Item")

我认为你想要的是:

ASP。净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。净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。净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. 您仍然定义了以下路由
< p >。 . < / p >
routes.MapRoute(
"Default",     // Route name
"{controller}/{action}/{id}",                           // URL with parameters
new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

你可能想看看RouteLink()方法。它允许您通过字典指定所有内容(除了链接文本和路由名称)。

我想添加到约瑟夫·金怒的回答。他提供了解决方案,但一开始我也不能让它工作,并得到了像Adhip Gupta一样的结果。然后我意识到,路由必须首先存在,参数必须与路由完全匹配。所以我有一个id,然后一个文本参数我的路线,这也需要包括在内。

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

我认为Joseph翻转了控制器和动作。首先是动作,然后是控制器。这个签名看起来有点奇怪。

为了澄清一些事情,这是一个有效的版本(改编自约瑟夫的例子):

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

这个呢

<%=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>

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

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

你们可以从中得到启发…

为了可读性和避免混淆,使用命名参数。

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

这种类型使用:

@Html.ActionLink(“主页”、“指数”,“家”)

MainPage:文本的名称 索引:动作视图 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>