我,为什么这个代码很迷茫为什么Html.ActionLink渲染
Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" })
结果在这个环节 “长度= 4?”:
<a hidefocus="hidefocus" href="/Home/About?Length=4">About</a>
的hidefocus
部分是我旨在实现,但?Length=4
从哪里来?
我,为什么这个代码很迷茫为什么Html.ActionLink渲染
Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" })
结果在这个环节 “长度= 4?”:
<a hidefocus="hidefocus" href="/Home/About?Length=4">About</a>
的hidefocus
部分是我旨在实现,但?Length=4
从哪里来?
Length = 4来自尝试序列化字符串对象。你的代码运行此ActionLink
方法:
public static string ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes)
这需要一个string
对象“家庭”为routeValues,这对于公众性的MVC水暖搜索他们变成路径的值。在string
对象的情况下,唯一的公共属性是Length
,并且由于不会有使用Length参数定义的路由,因此它将属性名称和值附加为查询字符串参数。您可能会发现,如果您从不在HomeController
的页面运行此操作,它会抛出关于缺少About
操作方法的错误。请尝试使用以下:
Html.ActionLink("About", "About", new { controller = "Home" }, new { hidefocus = "hidefocus" })
在下面看@ jesse的答案,它有一个(恕我直言)更好的解决方案... – THBBFT
@Blah_Blah这些天我不使用这个,更喜欢写我自己的标签,只使用Url.Action(“Action” ,“Controller”) – roryf
@ roryf无后顾之忧......这被用在代码审查中作为最佳实践,他们没有检查日期。真的应该有一种方法来过滤日期的计算器... – THBBFT
ActionLink的参数不正确,它试图使用“Home”值作为路由值,而不是匿名类型。
我相信你只需要添加new { }
或null
作为最后一个参数。
编辑:只是重新阅读这篇文章,并意识到你可能想要指定null作为第二个参数,而不是最后一个。
恕我直言,这是SOOOO吓人的烦人。每天发生10次。 – Jabe
告诉我这件事。 – 2009-05-05 10:47:53
顺便说一句,他们应该删除一种方法,因为它不可能使用它! –
我解决了,这是被匿名声明(new {}
)之前添加空的第四个参数的方式,以便它使用以下方法重载:(LINKTEXT,actionName,controllerName,routeValues ,htmlAttributes):
Html.ActionLink("About", "About", "Home", null, new { hidefocus = "hidefocus" })
这是最好的答案,并且应该被标记为答案。 – MoXplod
同样适用于我,谢谢 –
如果您没有连接区域,这是最好的答案。如果您需要指定链接的区域,则需要使用'controller'和'area'指定routeValues,以防止URL中显示Length属性。 –
只是删除 “家” 的控制器(名称),这样的代码将是:
Html.ActionLink("About", "About", new { hidefocus = "hidefocus" })
你忘了添加HTMLAttributes parm。
这将工作没有任何改变:
Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" },null)
谢谢Jesse Rose,它帮助了! –
+1这个实际上适合我!谢谢! –
这是什么为我工作 –
正如乔纳森·沃特尼在评论中指出,这也无二
Html.BeginForm()
方法。在我的情况下,我是在一个Create.cshtml靶向相应控制器的发布请求+创建动作,不得不
using (Html.BeginForm("Create")) {
@Html.AntiForgeryToken()
...
}
其添加查询字符串“?长度= 6”到表单动作当呈现。通过roryf暗示的批准的答案,实现“创造”的字符串长度为6,我最后,除去明确的操作规范解决了这个:
using (Html.BeginForm()) {
@Html.AntiForgeryToken()
...
}
Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" }, new { })
这将需要过载: 串LINKTEXT,串actionName ,串controllerName,对象routeValues,对象htmlAttributes
随着属性名称:
@Html.ActionLink(linkText: "SomeText", actionName: "SomeAction", controllerName: "SomeControllerName", routeValues: new { parameterName = parameterValue}, htmlAttributes: null)
请使用权重载甲基具有五(5)个参数。例如:
@using (@Ajax.BeginForm("Register", "Account", null,
new AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "OnSuccess",
OnFailure = "OnFailure",
OnBegin = "OnBegin",
OnComplete = "OnComplete"
}, new { @class = "form-login" }))
添加** null **控制器名称后有助于我的情况。 –
这与@ Jesse的答案实际上是一样的答案,它包含了Ajax版本的附加奖励,这不是OP所要求的,但它与相同的问题有关。 – gcoulby
此行为也与Html.BeginForm()方法一起出现。 –
这一直让我在一天中的大部分时间都精神失常。我大大松了一口气。 – 5arx