Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 356
Pedre Avatar asked Feb 20 '23 17:02

Pedre


2 Answers

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  } })%>
like image 167
Darin Dimitrov Avatar answered May 01 '23 17:05

Darin Dimitrov


There's no overload that matches your parameters, you should either use object for route and html or RouteValueDictinary and IDictionary<string,object>.

Like so:

Html.ActionLink(SharedResources.Shared_Pagination_First, Model.ActionToExecute, Model.ControllerToExecute, Model.FirstRouteValues, new Dictionary<string.object> { { "title", somevalue  } })
like image 33
Steen Tøttrup Avatar answered May 01 '23 17:05

Steen Tøttrup