Gra*_*ton 241 .net c# asp.net-mvc html-helper actionlink
假设我有一堂课
public class ItemController:Controller
{
public ActionResult Login(int id)
{
return View("Hi", id);
}
}
Run Code Online (Sandbox Code Playgroud)
在不在Item文件夹中的页面上ItemController,我想创建一个指向该Login方法的链接.那么Html.ActionLink我应该使用哪种方法以及我应该传递哪些参数?
具体来说,我正在寻找替代方法
Html.ActionLink(article.Title,
new { controller = "Articles", action = "Details",
id = article.ArticleID })
Run Code Online (Sandbox Code Playgroud)
已经在最近的ASP.NET MVC化身中退役了.
Jos*_*gry 478
我想你想要的是这个:
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).
)
Run Code Online (Sandbox Code Playgroud)
这使用以下方法ActionLink签名:
public static string ActionLink(this HtmlHelper htmlHelper,
string linkText,
string controllerName,
string actionName,
object values,
object htmlAttributes)
Run Code Online (Sandbox Code Playgroud)
两个论点已被切换
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).
)
Run Code Online (Sandbox Code Playgroud)
这使用以下方法ActionLink签名:
public static string ActionLink(this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
object values,
object htmlAttributes)
Run Code Online (Sandbox Code Playgroud)
参数与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).
)
Run Code Online (Sandbox Code Playgroud)
这避免了将任何路由逻辑硬编码到链路中.
<a href="/Item/Login/5">Title</a>
Run Code Online (Sandbox Code Playgroud)
这将为您提供以下html输出,假设:
article.Title = "Title"article.ArticleID = 5..
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)
Jef*_*mer 30
我想补充约瑟夫金瑞的回答.他提供了解决方案,但起初我无法让它工作,并得到像Adhip Gupta一样的结果.然后我意识到路线必须首先存在,参数需要与路线完全匹配.所以我有一个id,然后是我的路线的文本参数,也需要包含它.
Html.ActionLink(article.Title, "Login", "Item", new { id = article.ArticleID, title = article.Title }, null)
Run Code Online (Sandbox Code Playgroud)
Haa*_*ked 17
您可能希望查看该RouteLink()方法.您可以通过字典指定所有内容(链接文本和路由名称除外).
age*_*gez 14
我认为约瑟夫打开了控制器和动作.首先是动作然后是控制器.这有点奇怪,但签名的样子.
只是为了澄清事情,这是有效的版本(适应约瑟夫的例子):
Html.ActionLink(article.Title,
"Login", // <-- ActionMethod
"Item", // <-- Controller Name
new { id = article.ArticleID }, // <-- Route arguments.
null // <-- htmlArguments .. which are none
)
Run Code Online (Sandbox Code Playgroud)
小智 11
那这个呢
<%=Html.ActionLink("Get Involved",
"Show",
"Home",
new
{
id = "GetInvolved"
},
new {
@class = "menuitem",
id = "menu_getinvolved"
}
)%>
Run Code Online (Sandbox Code Playgroud)
Adh*_*pta 10
Html.ActionLink(article.Title, "Login/" + article.ArticleID, 'Item")
Run Code Online (Sandbox Code Playgroud)
如果你想要所有的花式裤子,这里是你可以扩展它以便能够做到这一点:
@(Html.ActionLink<ArticlesController>(x => x.Details(), article.Title, new { id = article.ArticleID }))
Run Code Online (Sandbox Code Playgroud)
您需要将其放在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;
}
}
Run Code Online (Sandbox Code Playgroud)
这包括两个覆盖,Route Values以及HTML Attributes您需要添加的所有视图:@using YourProject.Controllers或者您可以将其添加到您的视图中web.config <pages><namespaces>
使用命名参数以获得最佳可读性并避免混淆:
@Html.ActionLink(
linkText: "Click Here",
actionName: "Action",
controllerName: "Home",
routeValues: new { Identity = 2577 },
htmlAttributes: null)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
677162 次 |
| 最近记录: |