【问题标题】:list of checkbox “true,false” value MVC 3复选框“真,假”值MVC 3的列表
【发布时间】:2012-10-17 21:16:28
【问题描述】:

我在视图中

@model IEnumerable<RolesMVC3.Models.Estudent>
.
.
.
    @for (var i = 0; i < Model.Count(); i++)
    { 
              <tr>
                <td> @Html.CheckBox("CheckValue")</td>
                <td> @Html.DisplayFor(m => m[i].CodeEstudent)  @Html.HiddenFor(m => m[i].IdEstudent)</td>
                <td>@Html.DisplayFor(m => m[i].NameEstudent)  @Html.DisplayFor(m => m[i].LastNameEstudent)</td>
            </tr>     
     }
.
.
.

在控制器中:

[HttpPost]
    public ActionResult MyController(List<ESTUDENT> estudents, List<bool> CheckValue)
    {

        ///Actions
    }

但是,我为每个学生收到了两个 CheckBox。

例如我在控制器中发送 29 并接收 58

如何将 CheckBox 与此列表中的学生关联并进入控制器?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3


    【解决方案1】:

    首先,你需要了解 ASP.NET MVC 是如何渲染复选框的:

    <input id="RememberMe" type="checkbox" value="true" name="RememberMe" />
    <input type="hidden" value="false" name="RememberMe" />
    

    它是如何工作的?表单始终提交 hidden 字段并仅在选中时提交 type="checkbox",然后 binder 查看类型 (bool),如果有两个值则设置为 true,否则为 false

    在您的示例中,您需要将 index 设置为 CheckBox,因此您将发送 29 对数据,而不是 58 个独立值。

    更多细节:

    【讨论】:

      猜你喜欢
      • 2016-09-01
      • 2012-04-15
      • 2013-12-09
      • 2014-05-10
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多