15

I have found so many questions about this, but none of them go over or seem to go over my scenario. I have a model:

public class CheckBoxModel
{
            public int Id{ get; set; }
    public bool IsSelected { get; set;  }
}

In then try and bind my IsSelected bool to a checkbox like this:

<%= Html.CheckBox("IsSelectedCheck",Model.IsSelected)%>

I have lots of items on a page, they all have a checkbox next to them, all i am trying to do is pass back to the controller all the id's of the items and which ones have been selected.

At the moment, the value of IsSelected is always false. Should Html.CheckBox set the value of Model.IsSelected each time the user toggles the checkbox.

Thanks

TBD
  • 771
  • 1
  • 11
  • 27
  • Your model's property name is `IsSelected` but in your view, you've named it `IsSelectedCheck`. – Jalal May 17 '16 at 05:22

2 Answers2

29

Try like this:

<%= Html.CheckBoxFor(x => x.IsSelected) %>

Also if you want to pass along the id don't forget to do so:

<%= Html.HiddenFor(x => x.Id) %>

And if you had a collection of those:

public class MyViewModel
{
    public CheckBoxModel[] CheckBoxes { get; set; }
}

you could:

<% for (var i = 0; i < Model.CheckBoxes.Length; i++) { %>
    <div>
        <%= Html.HiddenFor(x => x.CheckBoxes[i].Id) %>
        <%= Html.CheckBoxFor(x => x.CheckBoxes[i].IsSelected) %>
    </div>
<% } %>

which will successfully bind to:

[HttpPost]
public ActionResult MyAction(MyViewModel model) 
{
    // model.CheckBoxes will contain everything you need here
    ...
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I have previously tried <%= Html.CheckBoxFor(x => x.IsSelected) %> also, but i had the same issue. This may be a silly question, but does this code have to be within a form to work, because currently I'm using these checkboxes outside of a form. – TBD Mar 06 '12 at 09:41
  • @Marcel, of course that it has to be inside a form. How do you expect the values to be sent to the server if you don't put them inside a form? You could do that using AJAX but you will have to write javascript for that. – Darin Dimitrov Mar 06 '12 at 09:44
  • I probably didnt explain myself properly, I do have form in which i submit the data. That works, I just meant the checkbox itself isnt inside the form. Then when I do submit the form I do: <%= Html.Hidden("["+itemx+"].IsSelected", x.IsSelected) %> – TBD Mar 06 '12 at 09:52
  • @Marcel, I repeat my question: if the checkbox is not inside the form, how do you expect its value to be sent to the server? You seem to have put some hidden field inside the form, but obviously this hidden field has the value of the checkbox on the server, at the moment the page was rendered. The user can freely check/uncheck the checkbox which will not reflect ni any way the value you stored in your hidden field. So what you get when you postback is the value of the hidden field, which is the initial value, not the actual value of the checkbox that the user could modify. – Darin Dimitrov Mar 06 '12 at 09:55
  • I see, of course. Yeah i have changed the webpage now. That is working just fine, i was having a complete mare. Thanks again Darin. – TBD Mar 06 '12 at 11:11
  • what if multiple checkboxes are selected ? how to get multiple selected checkboxes in controller ? Please help – DharaPPatel Jun 10 '13 at 13:52
  • @DharaPPatel, you could read them from the view model. The POST controller action takes a view model containing an array of `CheckBoxModel` property. You could then look at each element and you will know whether it was selected or not. – Darin Dimitrov Jun 10 '13 at 14:54
  • where to write all this :( i am new to MVC.. what is CheckBoxModel[] ?? – DharaPPatel Jun 11 '13 at 05:47
  • @DharaPPatel, the `CheckBoxModel` type is shown in the original question. – Darin Dimitrov Jun 11 '13 at 06:30
  • but then if i am using ViewModel i cant use more then one model in my view right ? – DharaPPatel Jun 11 '13 at 06:39
22

An alternative to Darin's fantastic answer

I definitely recommend following Darin's approach for returning classes which will be most of the time. This alternative is a 'quick' and dirty hack if all you need is the checked Ids:

<% foreach (var cb in Model.CheckBoxes) { %>
  <div>
    <input type="checkbox" 
      value="<%= cb.Id %>"
      <%= cb.IsSelected ? "checked=\"checked\"" : "" %>
      name="ids" />
  </div>
<% } %>

Will bind to the int[] ids parameter in the following action:

[HttpPost]
public ActionResult MyAction(int[] ids) 
{
    // ids contains only those ids that were selected
    ...
}
  • The benefit is cleaner html as there is no hidden input.
  • The cost is writing more code in the view.

In MVC 4.0 (Razor 2.0) you can use the following syntax in your view:

<input type="checkbox" value="@cb.Id" checked="@cb.IsSelected" name="ids" />
Scotty.NET
  • 12,533
  • 4
  • 42
  • 51