0

I want to use CheckBoxFor() rather <input type="checkbox"/> I used the input like this:

<input type="checkbox" name="selectedItems" value="@item.Name" id="@item.Id"/>

here I used the checkbox to hold some values if selected, and then passed them to the controller in a string[] parameter (it's contained within foreach loop) but how I implement the CheckBoxFor to behave the same, as I know so far, we can only pass a boolean expression as the first parameter?

Mohamed
  • 73
  • 1
  • 12
  • you have to use jquery for that, or use the bool property with checkbox and in controller only process the string array if bool comes as true, which means checkbox checked – Ehsan Sajjad Nov 20 '16 at 20:47
  • `CheckBoxFor()` is for binding to `boolean` properties. If you want a checked list box, refer [this answer](http://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) for an example –  Nov 20 '16 at 20:53
  • I'm wondering if there any advantage of using the HTML helpers instead of the raw HTML inputs other than validating inputs, because I don't need the checkbox to be validated? – Mohamed Nov 20 '16 at 21:03
  • One of main reasons is to get 2-way model binding –  Nov 21 '16 at 00:38

0 Answers0