RouteValueDictionary和htmlAttributes之间是否存在冲突?

Ped*_*dre 4 c# asp.net-mvc html-helper actionlink routevalues

我使用RouteValueDictionary将RouteValues传递给ActionLink:

如果我编码:

<%:Html.ActionLink(SharedResources.Shared_Pagination_First, Model.ActionToExecute, Model.ControllerToExecute, Model.FirstRouteValues, null)%>
Run Code Online (Sandbox Code Playgroud)

链接结果是Ok:

SearchArticles?refSearch=2&exact=False&manufacturerId=5&modelId=3485&engineId=-1&vehicleTypeId=5313&familyId=100032&page=0
Run Code Online (Sandbox Code Playgroud)

但如果我编码:

<%: Html.ActionLink(SharedResources.Shared_Pagination_First, Model.ActionToExecute, Model.ControllerToExecute, Model.FirstRouteValues, new { @title = string.Format(SharedResources.Shared_Pagination_LinkTitle, 0) })%>
Run Code Online (Sandbox Code Playgroud)

链接结果是:

SearchArticles?Count=10&Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D
Run Code Online (Sandbox Code Playgroud)

有什么问题?唯一的区别是,在最后我使用的是htmlAttributes

Dar*_*rov 7

您正在使用ActionLink帮助程序的错误重载.有没有重载需要routeValues作为RouteValueDictionaryhtmlAttributes作为一个匿名对象.因此,如果Model.FirstRouteValues是,RouteValueDictionary那么最后一个参数也必须是一个RouteValueDictionary 或一个简单的IDictionary<string,object>而不是一个匿名的对象.就像那样:

<%= Html.ActionLink(
    SharedResources.Shared_Pagination_First, 
    Model.ActionToExecute, 
    Model.ControllerToExecute, 
    Model.FirstRouteValues, 
    new RouteValueDictionary(
        new { 
            title = string.Format(SharedResources.Shared_Pagination_LinkTitle, 0) 
        }
    )
) %>
Run Code Online (Sandbox Code Playgroud)

要么

<%=Html.ActionLink(
SharedResources.Shared_Pagination_First, 
Model.ActionToExecute, 
Model.ControllerToExecute, 
Model.FirstRouteValues, 
new Dictionary<string, object> { { "title", somevalue  } })%>
Run Code Online (Sandbox Code Playgroud)