2012-07-27 51 views
4

我使用的是RouteValueDictionary到RouteValues傳遞給ActionLink的:RouteValueDictionary和htmlAttributes之間是否存在衝突?

如果我的代碼:

<%: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

回答

7

您正在使用ActionLink助手的錯誤重載。沒有超載,將routeValues作爲RouteValueDictionaryhtmlAttributes作爲匿名對象。因此,如果Model.FirstRouteValuesRouteValueDictionary那麼最後一個參數也必須是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 } })%> 
+0

非常感謝你 – Pedre 2012-07-27 10:13:16

+0

歡迎您。很高興我能幫上忙。 – 2012-07-27 10:13:32

1

有沒有超載符合您的參數,您應該使用object路線 HTML或RouteValueDictinaryIDictionary<string,object>

像這樣:

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

非常感謝 – Pedre 2012-07-27 10:13:31

相關問題