CheckBoxFor values not binding when output using foreach loop

If your outputting a list of checkboxes using CheckBoxFor from within a loop and are having problems getting the checkboxes to bind back when submitting make sure you are looping with a for loop rather than a foreach loop. This is because looping through a collection with a foreach loop will output the same name for all checkboxes which will be based on the name of the iteration variable (not the particular item in the viewModel collection) in your foreach statement and the name of the bool property and thus MVC will not find anything to bind to.

image008

One thought on “CheckBoxFor values not binding when output using foreach loop”

  1. Great solution to the problem thank you very much helps tons. Works with @using (Html.BeginCollectionItem) in partial views

Leave a Reply

Your email address will not be published. Required fields are marked *