4
 Html.CheckBox("SelectedStudents", false, new { @class = "check-item", id = x.Id, value = x.Id })

which produce

<input checked="checked" class="check-item" id="4507" name="SelectedStudents" value="4507" type="checkbox">

<input checked="checked" class="check-item" id="4507" name="SelectedStudents" value="4508" type="checkbox">

<input checked="checked" class="check-item" id="4507" name="SelectedStudents" value="4509" type="checkbox">

In mvc model I have

public IEnumerable<string> SelectedStudents { get; set; }

but when I post back, SelectedStudents are always null. Why? In this howto http://benfoster.io/blog/checkbox-lists-in-aspnet-mvc is written:

The ASP.NET MVC modelbinder is smart enough to map the selected items to this property.

but in my example is always null. Why? How to write more checkboxes and bind it back

1
  • Because the blog you posted is using a strongly typed Editor (strongly typed editors end in For like CheckListBoxFor) and you changed it to use a dumb editor (Checkbox). Dumb editors can be model bound back as long as you know exactly what you are doing. Commented Dec 28, 2015 at 17:17

4 Answers 4

12

You should be using a strongly typed editor to be able to pass the result to the controller (Model binder).

I prefer to do it this way.

Model

public class YourViewModel
{
     public List<SelectListItem> Students
        {
            get;
            set;
        }
}

Controller Get

Students= service.GetStudents(); //Fill the list

View

  @for (var i = 0; i < Model.Students.Count; i++)
                {

                    @Html.CheckBoxFor(m => m.Students[i].Selected)
                    @Html.HiddenFor(m => m.Students[i].Text)
                    @Html.HiddenFor(m => m.Students[i].Value)
                    <span>@Model.Students[i].Text</span>
                }

Controller Post

[HttpPost]
        public ActionResult Create(YourViewModel model)
        {
          foreach(var student in model.Students)
          {
            if(student.Selected) { // Do your logic}
          }
        }

Alternatively You could use an array or List of string. A ListBox is used in this example.

public string[] SelectedStudents{ get; set; }

@Html.ListBoxFor(s => s.SelectedStudents, new MultiSelectList(Model.Students, "Value", "Text", Model.SelectedStudents), new { @class = "form-control", style = "height:250px; width:100%" })
Sign up to request clarification or add additional context in comments.

Comments

1

See my answer here How to bind checkbox values to a list of ints?.

The nice thing about this is that it separates concerns between your controller and ui nicely. The html extension methods also create correct html using label and input for the checkbox. and there is no need for hidden fields.

Comments

0

Do you try it with CheckBoxListFor?? You need to associate the checkbox with model and should not have the same ID and name

@Html.CheckBoxListFor(model => model.SelectedSources, Model.SubscriptionSources)

Comments

0

You need to use a mutable type, like List<string>.

public List<string> SelectedStudents { get; set; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.