Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 829
ArdAtak Avatar asked Oct 25 '12 20:10

ArdAtak


1 Answers

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)
like image 67
Razvan Trifan Avatar answered Oct 09 '22 07:10

Razvan Trifan