Search code examples
c#asp.net-mvchtml-helperactionlinkroutevalues

Are there conflict between RouteValueDictionary and htmlAttributes?


I am using a RouteValueDictionary to pass RouteValues to a ActionLink:

If I code:

<%:Html.ActionLink(SharedResources.Shared_Pagination_First, Model.ActionToExecute, Model.ControllerToExecute, Model.FirstRouteValues, null)%>

The link result is Ok:

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

But if i code:

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

The link result is:

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

What's the problem? The only difference is that in the last i am using htmlAttributes


Solution

  • You are using the wrong overload of the ActionLink helper. There's no overload that takes routeValues as a RouteValueDictionary and htmlAttributes as an anonymous object. So if Model.FirstRouteValues is a RouteValueDictionary then the last argument must also be a RouteValueDictionary or a simple IDictionary<string,object> and not an anonymous object. Just like that:

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

    or

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