0

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?

12
  • check this: stackoverflow.com/a/16688321/3514288 Commented Jul 21, 2015 at 17:09
  • 1
    You really should use a strongly typed helper @Html.CheckBoxFor(model => model.personVM.IsShellFish) Commented Jul 21, 2015 at 18:26
  • 1
    As a side note, you should rename those allergy properties. IsShellfish implies 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. HasShellfishAllergy is much clearer. Commented Jul 21, 2015 at 22:20
  • @AlexArt. this is kinda late. I tried what you said and got the following errors: Error 11 Cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?) Commented Aug 4, 2015 at 14:13
  • and alsoError 12 Cannot convert lambda expression to delegate type 'System.Func<PrgComp_Competition.ViewModel.RegisterStudentViewModel,bool>' because some of the return types in the block are not implicitly convertible to the delegate return type Commented Aug 4, 2015 at 14:13

0

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.