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

4

2 回答 2

7

您使用了 ActionLink 帮助程序的错误重载。没有routeValues作为匿名对象RouteValueDictionary的重载。htmlAttributes所以如果Model.FirstRouteValues是 aRouteValueDictionary那么最后一个参数也必须是一个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) 
        }
    )
) %>

或者

<%=Html.ActionLink(
SharedResources.Shared_Pagination_First, 
Model.ActionToExecute, 
Model.ControllerToExecute, 
Model.FirstRouteValues, 
new Dictionary<string, object> { { "title", somevalue  } })%>
于 2012-07-27T10:03:39.080 回答
1

没有与您的参数匹配的重载,您应该使用objectroutehtml 或RouteValueDictinaryand IDictionary<string,object>

像这样:

Html.ActionLink(SharedResources.Shared_Pagination_First, Model.ActionToExecute, Model.ControllerToExecute, Model.FirstRouteValues, new Dictionary<string.object> { { "title", somevalue  } })
于 2012-07-27T10:05:44.043 回答