我很困惑为什么这个代码
Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" })
此链接的结果:
<a hidefocus="hidefocus" href="/Home/About?Length=4">About</a>
隐焦部分是我的目标,但?Length=4从何而来?
我很困惑为什么这个代码
Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" })
此链接的结果:
<a hidefocus="hidefocus" href="/Home/About?Length=4">About</a>
隐焦部分是我的目标,但?Length=4从何而来?
当前回答
属性名:
@Html.ActionLink(linkText: "SomeText", actionName: "SomeAction", controllerName: "SomeControllerName", routeValues: new { parameterName = parameterValue}, htmlAttributes: null)
其他回答
属性名:
@Html.ActionLink(linkText: "SomeText", actionName: "SomeAction", controllerName: "SomeControllerName", routeValues: new { parameterName = parameterValue}, htmlAttributes: null)
ActionLink的参数不正确,它试图使用“Home”值作为路由值,而不是匿名类型。
我相信你只需要添加new{}或null作为最后一个参数。
编辑:只是重新阅读了这篇文章,并意识到您可能希望将null指定为第二个最后一个参数,而不是最后一个参数。
也许其他人也有同样的问题,需要通过HTMLAttributes parm提供一个类值。 以下是我的解决方案:
@Html。ActionLink("About", "About", new {controller = "Home", area = ""}, new {hidefocus = "hidefocus", @class = "nav-item nav-link"})
我解决这个问题的方法是在匿名声明(new{})之前添加一个null到第四个参数,以便它使用以下方法重载:(linkText, actionName, controllerName, routeValues, htmlAttributes):
Html.ActionLink("About", "About", "Home", null, new { hidefocus = "hidefocus" })
Length=4来自于试图序列化一个字符串对象。你的代码正在运行这个ActionLink方法:
public static string ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes)
它接受一个字符串对象“Home”作为routeValues, MVC管道会搜索公共属性,将它们转换为路由值。在字符串对象的情况下,唯一的公共属性是Length,由于没有使用Length参数定义的路由,它将属性名和值作为查询字符串参数添加。你可能会发现,如果你从一个不是在HomeController上运行这个页面,它会抛出一个关于缺少about操作方法的错误。试着使用以下方法:
Html.ActionLink("About", "About", new { controller = "Home" }, new { hidefocus = "hidefocus" })