I previously had a view that contained as follows:
<tr>
<th class="editAddLabel">
@Html.LabelFor(model => model.IsShellFish, "Allergic to Shellfish")
</th>
<td class="editAddField">
@Html.CheckBox("IsShellFish")
</td>
</tr>
<tr>
<th class="editAddLabel">
@Html.LabelFor(model => model.IsPeaNut, "Allergic to Peanuts")
</th>
<td class="editAddField">
@Html.CheckBox("IsPeaNut")
</td>
</tr>
and it worked perfectly. I then made some changes to my project and wanted to use two different models in one view so I put the original model that was binded to the view and a second model in a ViewModel class, and eventually changed those lines of code to the following:
<tr>
<th class="editAddLabel">
@Html.LabelFor(model => model.personVM.IsShellFish, "Allergic to Shellfish")
</th>
<td class="editAddField">
@Html.CheckBox("personVM.IsShellFish")
</td>
</tr>
<tr>
<th class="editAddLabel">
@Html.LabelFor(model => model.personVM.IsPeaNut, "Allergic to Peanuts")
</th>
<td class="editAddField">
@Html.CheckBox("personVM.IsPeaNut")
</td>
</tr>
All my non-checkbox model properties are displaying normally and seem unaffected, but the checkbox ones do not display properly on the view whenever I want a user to edit their information. I know the properties are being passed properly from the view to the model when checked, and when I send the viewmodel back to the view it shows the correct boolean values for the fields corresponding to the views, so I feel like it must be something in my view's code that results in all the checkboxes being unchecked despite the fact that their values might be true. What could possibly be the problem?
@Html.CheckBoxFor(model => model.personVM.IsShellFish)IsShellfishimplies that the person may be a shellfish. I know this is a silly remark, but it's best practice to make your code self-documenting.HasShellfishAllergyis much clearer.