Question

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.

Was it helpful?

Solution

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)

OTHER TIPS

Im trying many ways but no one dont help me, eventually i`m himself solved the problem, just "unpack" your object on variables from object, like this:

@{ var sendObject = (MyObject) ViewData["SendObject"]; }
...
@Html.ActionLink("Description", "MyActionName", "MyController", new { sendObject.var1, sendObject.var2, sendObjec.var3, sortType = "Week" }, new { @style = "color: white" })

Controller:

public ActionResult MyActionName(MyObject sendObject, string sortType)
{
...
}

Framework himself wil bind your object SendObject

Just do this:

VIEW:
@Html.ActionLink("Link text", "Action name", "Controller name",
                        new { paramName1 = @modelDataItem.value, paramName2 = @modelDataItem.value}, null)

CONTROLLER:
public ActionResult ActionName(string paramName1 , string paramName2 )

Nb: ensure that the parameter names on the controller actions is same as that used in the action link. If the values are not delivered to the action params, then check whether the view's models has contains the value you are looking for. Perhaps it was never sent to the view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top