2010-05-24 154 views
10

任何人都可以解释为什么会发生以下情况吗?而如何解决,Visual Studio 2010和MVC2Asp.Net MVC ActionLink

<%= Html.ActionLink("Add New Option", "AddOption", "Product", new { @class = "lighbox" }, null)%> 

结果

/产品/ AddOption?CLASS =收藏

<%= Html.ActionLink("Add New Option", "AddOption", "Product", new { @class = "lighbox" })%> 

结果

/Product/AddOption?Length = 7

感谢

回答

20

您使用的这些各自重载:

public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper, 
string linkText, 
string actionName, 
string controllerName, 
Object routeValues, 
Object htmlAttributes 
) 

来源:http://msdn.microsoft.com/en-us/library/dd504972.aspx

public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper, 
string linkText, 
string actionName, 
Object routeValues, 
Object htmlAttributes 
) 

来源:http://msdn.microsoft.com/en-us/library/dd492124.aspx

第一new { @class = "lighbox" }传递作为routeValues参数时它应该是htmlAttributes参数。

这类问题与MVC中使用的扩展方法相同。在有时可以帮助使用命名的参数(C#4.0),以使事情更易读:

<%= Html.ActionLink(linkText: "Add New Option", 
    actionName: "AddOption", 
    controllerName: "Product", 
    htmlAttributes: new { @class = "lighbox" }, 
    routeValues: null)%> 
9

这是在ASP.NET MVC“超载地狱”的一个例子。

第一代码调用下面的方法:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper, 
    string linkText, 
    string actionName, 
    string controllerName, 
    Object routeValues, 
    Object htmlAttributes 
) 

,而第二代码调用这一:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper, 
    string linkText, 
    string actionName, 
    Object routeValues, 
    Object htmlAttributes 
) 

请注意,在第一次调用该字符串参数controllerName是变routeValues在第二一。将字符串值“Product”传递给路由值:使用字符串属性Length,其长度为7,因此您在路由中获得“Length = 7”。

考虑到第一种方法,您似乎已经交换了routeValueshtmlAttributes参数。