ActionLink with multiple parameters

Cameron picture Cameron · Apr 8, 2011 · Viewed 76.6k times · Source

I want to create a URL like /?name=Macbeth&year=2011 with my ActionLink which I have tried doing like so:

<%= Html.ActionLink("View Details", "Details", "Performances", new { name = item.show }, new { year = item.year })%>

but it doesn't work. How do I do this?

Answer

Mikael &#214;stberg picture Mikael Östberg · Apr 8, 2011

The overload you are using makes the year value end up in the html attributes of the link (check your rendered source).

The overload signature looks like this:

MvcHtmlString HtmlHelper.ActionLink(
    string linkText, 
    string actionName, 
    string controllerName, 
    object routeValues, 
    object htmlAttributes
)

You need to put both your route values in to the RouteValues dictionary like this:

Html.ActionLink(
    "View Details", 
    "Details", 
    "Performances", 
    new { name = item.show, year = item.year }, 
    null
)