1

I have in the view

@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>     
     }
.
.
.

In the controller:

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

        ///Actions
    }

But, I reciveb two CheckBox for each student.

e.g. I am sending 29 and receive 58 in the controller

How do I associate a CheckBox with a student on this list and get in the controller?

4

1 回答 1

3

首先,您需要了解 ASP.NET MVC 如何呈现复选框:

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

这个怎么运作?表单总是提交hidden字段并且type="checkbox"仅在它被选中时才提交,然后 binder 会查看类型 ( bool),如果它设置了两个值,则为trueelse false

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

更多细节:

于 2012-10-28T07:19:02.780 回答