简体   繁体   English

如何将值从View传递到Controller Action参数? MVC3

[英]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.

相关问题 如何使用jQuery将对象从MVC控制器动作传递到视图 - How to pass an object from a mvc controller action to a view using jquery jQuery MVC3如何从按钮提交传递参数并返回操作结果 - JQuery MVC3 How do I pass a parameter from a button submit and get the action result back 如何将值从一个控制器传递到ASP.Net MVC3中的另一个控制器 - How to pass values from One controller to another Controller in ASP.Net MVC3 将一个简单的字符串从控制器传递给视图 MVC3 - Pass a simple string from controller to a view MVC3 如何在mvc3的控制器中的动作的get方法中获取Formcollection值? - How to get Formcollection values in the get method of an action in a controller in mvc3? 如何从 mvc 中的 cshtml 视图将参数传递给 controller - How to pass a parameter to a controller from a cshtml view in mvc 将参数从View传递到控制器MVC 4 - Pass parameter from View to controller MVC 4 MVC将值/参数从控制器传递到视图 - MVC Pass value/parameter from controller to View 如何将包含IEnumerable模型(复杂)的模型从视图C#MVC3传递到控制器中? - How to pass a model containing an IEnumerable model (complex) into a controller from a view C# MVC3? MVC3控制器未收到任何参数值 - MVC3 Controller not receiving any parameter values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM