ASP.Net MVC复选框从视图中值自定义控制器的方法 [英] ASP.Net MVC checkbox values from view to custom controller method

查看:63
本文介绍了ASP.Net MVC复选框从视图中值自定义控制器的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示我的模型项的表的视图。我提取我的观点的相关部分:

I have a view with a table that displays my model items. I've extracted the relevant portions of my view:

@model System.Collections.Generic.IEnumerable<Provision>

@using (Html.BeginForm("SaveAndSend", "Provision", FormMethod.Post))
{
    if (Model != null && Model.Any())
    {
        <table class="table table-striped table-hover table-bordered table-condensed">
            <tr>
                ...
                // other column headers
                ...
                <th>
                    @Html.DisplayNameFor(model => model.IncludeProvision)
                </th>
                ...
                // other column headers
                ...
            </tr>
            @foreach (var item in Model)
            {
                <tr>
                    ...
                    // other columns
                    ...
                    <td>
                        @Html.CheckBoxFor(modelItem => item.IncludeProvision)
                    </td>
                    ...
                    // other columns
                    ...
                </tr>
            }
        </table>
        <button id="save" class="btn btn-success" type="submit">Save + Send</button>
    }
    ...
}

这工作正常,并在视 IncludeProvision 字段的给定的模型项目的布尔值的观点正确显示的复选框的值。

This works fine and the checkbox values are displayed correctly in the view depending on the boolean value of the IncludeProvision field for the given model item.

根据安德鲁·奥尔洛夫的回答,我已经修改了视图和控制器和 SaveAndSend()控制器方法现在是:

As per Andrew Orlov's answer, I've modified the view and controller and the SaveAndSend() controller method is now:

[HttpPost]
public ActionResult SaveAndSend(List<Provision> provisions)
{
    if (ModelState.IsValid)
    {
        // perform all the save and send functions
        _provisionHelper.SaveAndSend(provisions);
    }
    return RedirectToAction("Index");
}

不过,在这一点上的模型传递的对象为null。

However, at this point the passed in model object is null.

包括对完整性提供模型对象:

Including the Provision model object for completeness:

namespace
{
    public partial class Provision
    {
        ...
        // other fields
        ...
        public bool IncludeProvision { get; set; }
    }
}

我的问题是,什么是抓住每个复选框的选中/取消值,以及更新会话 IncludeProvision 字段为每个模型项目的最好方式,当SaveAndSend按钮被点击?

My question is, what is the best way to grab the checked/unchecked value from each checkbox and update the session IncludeProvision field for each model item when the 'SaveAndSend' button is clicked?

推荐答案

您不能使用的foreach 循环来生成表单控件集合中的属性。它创建复制名称属性(在你的案件 NAME =item.IncludeProvision),其中有你的模型没有任何关系和重复 ID 属性是无效的HTML。使用一个循环(你的模型必须是的IList&LT;提供&GT;

You cannot use a foreach loop to generate form controls for properties in a collection. It creates duplicate name attributes (in your case name="item.IncludeProvision") which have no relationship to your model and duplicate id attributes which is invalid html. Use either a for loop (you models needs to be IList<Provision>

for(int i = 0; i < Model.Count; i++)
{
  <tr>
    <td>....</td>
    <td>@Html.CheckBoxFor(m => m[i].IncludeProvision)<td>
  </tr>
}

或创建一个 EditorTemplate 为typeof运算提供。在 /Views/Shared/EditorTemplates/Provision.cshtml (注意模板的名称必须与类的名称相匹配)

or create an EditorTemplate for typeof Provision. In /Views/Shared/EditorTemplates/Provision.cshtml (note the name of the template must match the name of the type)

@model Provision
<tr>
  <td>....</td>
  <td>@Html.CheckBoxFor(m => m.IncludeProvision)<td>
</tr>

和在主视图(模型可的IEnumerable&LT;提供&GT;

<table>
  @Html.EditorFor(m => m)
</table>

这篇关于ASP.Net MVC复选框从视图中值自定义控制器的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆