[英]How to pass values from View to a Controller Action parameter? MVC3
I apologize if asking some questions these days. 我很抱歉最近问了一些问题。 But appreciate for all kind helps.
但是感谢所有帮助。 Actually, I need to pass data from View to Controller using MVC3 Razor.
实际上,我需要使用MVC3 Razor将数据从View传递到Controller。
Here is the View Code: 这是查看代码:
Html.RenderAction("Checks", Model.GetRoleNames);
Here is the Controller Code: 这是控制器代码:
public ActionResult Checks(string[] getRoleNames)
{
// The parameter is NULL which should NOT be!
//ViewBag.Name = name;
//return View(values);
return View();
}
Here is the Partial View Code: 这是部分查看代码:
@model string[]
@{
for (int i = 0; i < Model.Length; i++)
{
<input id="@ViewBag.Name@(i)" name="@ViewBag.Name" type="checkbox" value="@(Model[i])" />
<label for="@ViewBag.Name@(i)">@Model.[i]</label>
<br />
}
} }
The Problem: Model.GetRoleNames has values inside the View as I tried tracing line by line. 问题:当我尝试逐行跟踪时,Model.GetRoleNames在视图内具有值。 But no value is passed to the Controller Action as parameter!
但是不会将任何值作为参数传递给Controller Action! I don't know the possible reason.
我不知道可能的原因。
Can anyone help me please? 谁能帮我吗?
This method expects route parameters in a slightly different form: 此方法期望路由参数的形式略有不同:
Html.RenderAction("Checks", new {getRoleNames = Model.GetRoleNames});
Update. 更新。 As to why parameters should be provided in such a form one might refer to the description from MSDN :
至于为什么应该以这种形式提供参数,可以参考MSDN的描述:
routeValues
routeValues
An object that contains the parameters for a route.
包含路由参数的对象。 You can use routeValues to provide the parameters that are bound to the action method parameters.
您可以使用routeValues提供绑定到操作方法参数的参数。 The routeValues parameter is merged with the original route values and overrides them.
routeValues参数与原始路由值合并并覆盖它们。
The only thing to be added here is how exactly this object is bound to the input parameters. 这里唯一要添加的是该对象如何精确地绑定到输入参数。 Default model binder parses input object and tries to find correspondence between object's fields and names of Action's input parameters.
默认模型绑定程序解析输入对象,并尝试查找对象的字段和Action的输入参数名称之间的对应关系。 So in the case above
Action
had a single parameter named getRoleNames
, and that is the name model binder was looking for. 因此,在上述情况下,
Action
具有一个名为getRoleNames
参数,而这正是模型绑定程序正在寻找的名称。 Obviously object Model.GetRoleNames
does not contain such field/property, while new {getRoleNames = Model.GetRoleNames}
does. 显然,对象
Model.GetRoleNames
不包含此类字段/属性,而new {getRoleNames = Model.GetRoleNames}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.