Html.CheckBoxFor not functioning correctly and generating different html then mirrored copies

63 views Asked by At

I've looked around for similar issues, but couldn't find this particular problem. I suspect it's something easy to solve, but I've not seen it before and everything I've tried doesn't resolve it.

Using MVC 5, C# models and a Razor view. I have a model with roughly 20 or so bool entities that I use for checkboxes. A user initially selects their checkboxes as part of an initial form insert. The problem rests when doing an update.

18 out of the 20 checkboxes update like they should. Here is an example of one that works correctly. All 20 look exactly like this except the model names are different of course.

 <div class="form-group ">
      <label class="d-inline-flex  customcheckPsNp  pb-1">
           @Html.CheckBoxFor(model => model._GetCompleteGroup1.MvrInFile)
           <span class="checkmarkPsNp"></span>
      </label>
      <label for="MvrInFile" class="lblPolicyServiceList ml-1">MVR In File</label>
 </div>

Here is the generated html for that.

 <label class="d-inline-flex  customcheckPsNp  pb-1">
      <input data-val="true" data-val-required="The MvrInFile field is required." name="_GetCompleteGroup1.MvrInFile" type="checkbox" value="true">
      <input name="_GetCompleteGroup1.MvrInFile" type="hidden" value="false">
          <span class="checkmarkPsNp"></span>
 </label>

These checkboxes get sent to the controller and updated whether true or false. All but two of them.

Here is one of the offending Checkboxfors and the generated HTML. I suspect this is where the problem sits and the part I don't quite understand. The generated HTML is different from the others.

<div class="form-group ">
     <label class="d-inline-flex customcheckPsNp pb-1">
         @Html.CheckBoxFor(model => model._GetCompleteGroup1.LocInFile)
         <span class="checkmarkPsNp"></span>
      </label>
      <label for="LocInFile" class="lblPolicyServiceList ml-1">LOC In File</label>
</div>

Now the generated HTML found in the console elements

<label class="d-inline-flex customcheckPsNp pb-1">
       <input name="_GetCompleteGroup1.LocInFile" type="checkbox" value="true">
       <input name="_GetCompleteGroup1.LocInFile" type="hidden" value="false">
       <span class="checkmarkPsNp"></span>
</label>

As you can see the difference in the generated HTML is different. Does anyone know why and how to resolve this? I've tried manually creating the same html from above in place of this one, but that still doesn't work. I've looked at the model it all aligns. I would get an error if these bools weren't there. The controller accounts for bools within the model as well.

I suspect that these two check boxes are not being correctly sent with the model to the controller. Possibly due to the way the HTML is being generated. I could be way off, but it's the only clear thing that's different amongst the check boxes.

Grateful for the help.

1

There are 1 answers

0
swapmeet_Lou On

I was just going to delete the question, but felt worthy a good lesson for others and some humble pie for myself. I had actually done everything right in my models, controllers, and my view. Except one important thing. I had tripled the times I was using the same model entity. After looking in the console > network, I could see how many times the checkboxfor name was actually used. I had a different label so I didn't realize I fat fingered and chose the same model entity multiple times. My mistake. Always check the easiest first. :)