如何动态地将多个参数传递给Asp .Net MVC中的Html.Action

ser*_*lge 3 c# html-helper parameter-passing asp.net-mvc-4

我有参数发送像

@Html.Action("actionName", "controlName", new{ pName1 = "pValue1", ... })

但是,pName1 = "pValue1", ...将来自控制器的ViewBag.应该用ViewBag封装的对象类型,以及如何将路由值设置为Html.Action?

Ala*_*oud 5

对象的类型可以是您从原始类型(如int,string等)到自定义对象的任何类型.

如果您已为ViewBag分配了值,例如:

public class CustomType {
  public int IntVal { get; set; }
  public string StrVal { get; set; }
}
...
ViewBag.SomeObject = new CustomType { IntVal = 5, StrVal = "Hello" }
Run Code Online (Sandbox Code Playgroud)

您可以简单地调用它:

@Html.Action("SomeAction", "SomeController", new { myParam = @ViewBag.SomeObject })
Run Code Online (Sandbox Code Playgroud)

在你的控制器中:

public ActionResult SomeAction(CustomType myParam ) {
  var intVal = myParam.IntVal;
  var strVal = myParam.StrVal;
  ...
}
Run Code Online (Sandbox Code Playgroud)

但请注意,您仍然可以从控制器中访问ViewBag,而无需在路由值中传递它们.

这回答了你的问题了吗?