如何使用Html.ActionLink传递多个参数(一些复杂的对象)

时间:2021-12-20 15:15:30

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.

我在.net 4.0中使用MVC。我有一个基本的Html.ActionLink,我希望将多个参数传递给控制器​​/动作。

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.

每次我调试ActionResult时,只有一个参数通过而另一个是null(取决于哪个是第一个)。现在我知道我可以传递复杂的物体,因为我可以让“联盟”对象通过。但是,我不确定为什么我的参数中只有一个可以随时通过。

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

Code in View :(不要骚扰我ViewBag。我知道它不受欢迎。此外,联盟是一个复杂的对象)

@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.

我猜测答案将围绕路由。这让我想到了第二个问题。如何在不经常添加新maproutes的情况下使这种类型的ActionLink(动作/控制器/复杂和简单对象的集合)工作。是否有我可以添加的通用/通配符RouteMap,所以我不必经常在解剖学上将相同的路由映射添加到global.asax。或者我希望在我希望传递给Action的对象类型中有一些灵活性,因此我无法预定义确切的签名。

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

我在这个主题上看过多篇帖子,但没有一篇回答我的问题。

3 个解决方案

#1


4  

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

我认为由于null参数可能会让人感到困惑。尝试这个:

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

#2


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

框架本身将绑定您的对象SendObject

#3


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.

Nb:确保控制器操作上的参数名称与操作链接中使用的参数名称相同。如果值未传递给操作参数,则检查视图的模型是否包含您要查找的值。也许它从未被发送到视图中。

#1


4  

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

我认为由于null参数可能会让人感到困惑。尝试这个:

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

#2


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

框架本身将绑定您的对象SendObject

#3


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.

Nb:确保控制器操作上的参数名称与操作链接中使用的参数名称相同。如果值未传递给操作参数,则检查视图的模型是否包含您要查找的值。也许它从未被发送到视图中。