本文翻译自:Why does Html.ActionLink render “?Length=4”
I'm VERY confused as to why this code 我很困惑为什么这段代码
Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" })
results in this link: 结果在此链接:
<a hidefocus="hidefocus" href="/Home/About?Length=4">About</a>
The hidefocus
part is what I was aiming to achieve, but where does the ?Length=4
come from? hidefocus
部分是我想要实现的目标,但是?Length=4
来自何处?
#1楼
参考:https://stackoom.com/question/3SQp/为什么Html-ActionLink呈现-Length
#2楼
只需删除“ Home”(控制器的名称),使代码为:
Html.ActionLink("About", "About", new { hidefocus = "hidefocus" })
#3楼
You forgot to add the HTMLAttributes parm. 您忘记添加HTMLAttributes参数。
This will work without any changes: 这将无需任何更改即可工作:
Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" },null)
#4楼
As Jonathon Watney pointed out in a comment, this also goes for 正如乔纳森·沃特尼(Jonathon Watney)在评论中指出的那样,
Html.BeginForm() Html.BeginForm()
methods. 方法。 In my case, I was in a Create.cshtml targeting the post request of the corresponding controller + Create action and had 就我而言,我在一个Create.cshtml中,定位了相应控制器+ Create操作的发布请求,
using (Html.BeginForm("Create")) {
@Html.AntiForgeryToken()
...
}
which was adding the querystring "?Length=6" to the form action when rendered. 呈现时将查询字符串“?Length = 6”添加到表单操作中。 Hinted by roryf's approved answer and realizing the string length of "Create" is 6, I finally solved this by removing the explicit action specification: 在roryf批准的答案的提示下,并意识到“ Create”的字符串长度为6,我终于通过删除显式操作规范解决了这个问题:
using (Html.BeginForm()) {
@Html.AntiForgeryToken()
...
}
#5楼
Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" }, new { })
这将导致重载:字符串linkText,字符串actionName,字符串controllerName,对象routeValues,对象htmlAttributes
#6楼
具有属性名称:
@Html.ActionLink(linkText: "SomeText", actionName: "SomeAction", controllerName: "SomeControllerName", routeValues: new { parameterName = parameterValue}, htmlAttributes: null)