Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionLink with multiple parameters

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?

like image 638
Cameron Avatar asked Apr 08 '11 10:04

Cameron


People also ask

What is difference between HTML ActionLink and URL action?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.

How do I transfer my ActionLink model to my controller?

You'll have to serialize your model as a JSON string, and send that to your controller to turn into an object. Please don't format with snippets when the language used is not displayable by them. It doesn't improve the answer and makes your answer more cluttered. Regular code blocks will work fine.


2 Answers

In addition to Mikael Östberg answer add something like this in your global.asax

routes.MapRoute(     "View Details",     "Performances/Details/{name}/{year}",     new {         controller ="Performances",         action="Details",          name=UrlParameter.Optional,         year=UrlParameter.Optional     }); 

then in your controller

// the name of the parameter must match the global.asax route     public action result Details(string name, int year) {     return View();  } 
like image 39
hidden Avatar answered Sep 20 '22 06:09

hidden


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 ) 
like image 130
Mikael Östberg Avatar answered Sep 20 '22 06:09

Mikael Östberg