How to pass multiple parameters (some complex objects) with Html.ActionLink

ArdAtak picture ArdAtak · Oct 25, 2012 · Viewed 7.1k times · Source

I'm working with MVC in .net 4.0. I have a basic Html.ActionLink and I wish to pass multiple parameters to the controller/action.

Every time I debug the ActionResult only ONE of my parameters comes through and the other is null (depending on which is first). Now I know I can pass in complex objects since I can get the "League" object to come through. However, I'm not sure why only ONE of my parameters makes it through at any time.

Code in View: (don't harass me about ViewBag. I know it's not popular. Also, League is a complex object)

@Html.ActionLink("Sort By WeeK", "Sort", "Schedule",
                 new { league = ViewBag.League, sortType = "Week" }, null)

Code in Controller: (no surprises here)

public ActionResult Sort(League league, string sortType)
{
    //Do some stuff here
    return View("Schedule");
}

I'm guessing the answer will revolve around routing. Which brings me to my 2nd question. How can I get this type of ActionLink (Action / Controller / Collection of Complex and Simple objects) to work without constantly adding new maproutes. Is there a generic / wildcard RouteMap I could add so I don't have to constantly add anatomically identical route maps to global.asax. Or maybe I want some flexibility in the type of objects I wish to pass into an Action so I can't predefine the exact signature.

I've seen multiple posts on this topic but none of them answered my questions.

Answer

Razvan Trifan picture Razvan Trifan · Oct 25, 2012

I think it might be getting confused because of the null parameter. Try this:

@Html.ActionLink("Sort By WeeK", actionName:"Sort", controllerName: "Schedule", routeValues: new { league = ViewBag.League, sortType = "Week" }, htmlAttributes:null)