kalu
kalu

Reputation: 337

list of checkbox “true,false” value MVC 3

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?

Upvotes: 1

Views: 3742

Answers (1)

webdeveloper
webdeveloper

Reputation: 17288

First of all, you need to understand how ASP.NET MVC render checkbox:

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

How it works? Form always submit hidden field and submit type="checkbox" only if it was checked, then binder look's at the type (bool) and if there is two values it set true, else false.

In your example you need to set index for CheckBox, so you will send 29 pairs of data, not 58 independent values.

More details:

Upvotes: 3

Related Questions