[英]Asp.Net MVC ActionLink
Can anyone explain why the following happens? 谁能解释为什么会发生以下情况? And how to resolve, Visual Studio 2010 and MVC2 以及如何解决,Visual Studio 2010和MVC2
<%= Html.ActionLink("Add New Option", "AddOption", "Product", new { @class = "lighbox" }, null)%>
Results in 结果是
/Product/AddOption?class=lightbox /产品/ AddOption?CLASS =收藏夹
<%= Html.ActionLink("Add New Option", "AddOption", "Product", new { @class = "lighbox" })%>
Results in 结果是
/Product/AddOption?Length=7 /产品/ AddOption?长度= 7
Thanks 谢谢
You're using these respective overloads: 您正在使用这些相应的重载:
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
Object routeValues,
Object htmlAttributes
)
From: http://msdn.microsoft.com/en-us/library/dd504972.aspx 来自: http : //msdn.microsoft.com/en-us/library/dd504972.aspx
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
Object routeValues,
Object htmlAttributes
)
From: http://msdn.microsoft.com/en-us/library/dd492124.aspx 来自: http : //msdn.microsoft.com/en-us/library/dd492124.aspx
The first new { @class = "lighbox" }
is passed as the routeValues
argument when it should be the htmlAttributes
argument. 第一new { @class = "lighbox" }
作为被传递routeValues
参数时,它应该是htmlAttributes
参数。
This sort of problem is common with the extension methods used in MVC. 这种问题在MVC中使用的扩展方法很常见。 In can sometimes help to use named arguments (C# 4.0) to make things more readable: In有时可以帮助使用命名参数 (C#4.0)来使事情更具可读性:
<%= Html.ActionLink(linkText: "Add New Option",
actionName: "AddOption",
controllerName: "Product",
htmlAttributes: new { @class = "lighbox" },
routeValues: null)%>
This is an example of "overload hell" in ASP.NET MVC. 这是ASP.NET MVC中“重载地狱”的一个例子。
The first code calls the following method: 第一个代码调用以下方法:
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
Object routeValues,
Object htmlAttributes
)
whereas the second code calls this one: 而第二个代码称之为:
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
Object routeValues,
Object htmlAttributes
)
Notice that the string parameter controllerName
in the first call is becoming routeValues
in the second one. 请注意,第一个调用中的字符串参数controllerName
正在成为第二个调用中的routeValues
。 The string value "Product" is being passed to the routed values: the string property Length
is used, which has a length of 7 here, hence the "Length=7" you're getting in the route. 字符串值“Product”被传递给路由值:使用字符串属性Length
,这里长度为7,因此您在路径中获得“长度= 7”。
Considering the first method, it seems that you've swapped the routeValues
and htmlAttributes
parameters. 考虑到第一种方法,您似乎已经交换了routeValues
和htmlAttributes
参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.