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

4
我将使用RouteValueDictionary来传递RouteValues到ActionLink:
如果我编写以下代码:
<%:Html.ActionLink(SharedResources.Shared_Pagination_First, Model.ActionToExecute, Model.ControllerToExecute, Model.FirstRouteValues, null)%>

链接结果正常:

链接结果正常:

SearchArticles?refSearch=2&exact=False&manufacturerId=5&modelId=3485&engineId=-1&vehicleTypeId=5313&familyId=100032&page=0

但如果我编写代码:
<%: Html.ActionLink(SharedResources.Shared_Pagination_First, Model.ActionToExecute, Model.ControllerToExecute, Model.FirstRouteValues, new { @title = string.Format(SharedResources.Shared_Pagination_LinkTitle, 0) })%>

链接结果如下:
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

问题出在哪里?唯一的区别在于我在最后使用了htmlAttributes。

2个回答

7
您正在使用错误的ActionLink帮助程序重载。没有重载接受RouteValueDictionary和匿名对象作为htmlAttributes的routeValues。所以,如果Model.FirstRouteValues是RouteValueDictionary,则最后一个参数也必须是RouteValueDictionary或简单的IDictionary 而不是匿名对象。就像这样:
<%= Html.ActionLink(
    SharedResources.Shared_Pagination_First, 
    Model.ActionToExecute, 
    Model.ControllerToExecute, 
    Model.FirstRouteValues, 
    new RouteValueDictionary(
        new { 
            title = string.Format(SharedResources.Shared_Pagination_LinkTitle, 0) 
        }
    )
) %>

或者

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

1

没有符合您参数的重载,您应该使用object来表示路由HTML,或者使用RouteValueDictionaryIDictionary<string,object>

像这样:

Html.ActionLink(SharedResources.Shared_Pagination_First, Model.ActionToExecute, Model.ControllerToExecute, Model.FirstRouteValues, new Dictionary<string.object> { { "title", somevalue  } })

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接