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

3

6

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.

Naphthalene answered 25/10, 2012 at 20:57 Comment(2)
weird. Try FormCollection in your controller's action and see if it is returning multiple keys. Worst case senario you can turn it into a form and submit the hidden HTML.Ford
When you say "Which one is first", do you mean first in the method signature or first in the URL of the link? What does the generated URL look like?Hyperploid
H
4

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)
Hardship answered 25/10, 2012 at 21:50 Comment(2)
Thanks for the feedback. I will test this and get back to you. As the scope of the app expanded, the League object became a public property of the controller so I no longer needed to pass it in. I'll try your suggestion the next time I have to pass in more than one parameter and post the results here. Happy Friday.Naphthalene
Sorry for the late reply but I just saw this and figured I still owed a confirmation. The suggestion worked. Thanks.Naphthalene
F
0

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

Fleck answered 3/6, 2016 at 20:52 Comment(0)
C
0

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.

Criticize answered 5/3, 2017 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.