0

I wrote this in MVC view:

<td>
    @Html.CheckBoxFor(modelItem => Model.Selected[5])
</td>

It generates:

<td>
    <div class="checker" id="uniform-Selected_5_">
        <span class="checked">
            <input id="Selected_5_" name="Selected[5]" type="checkbox" value="true">
        </span>
    </div>
    <input name="Selected[5]" type="hidden" value="false">
</td>

When a user clicks on the checkbox, the span value changes, but not checkbox value.

With the Javascript below, I change the value of the checkbox, but the span value doesn't change.

var name = "Selected_5_";
document.getElementById(name).checked = 1;
That guy
  • 135
  • 2
  • 10
  • This is a qwirk of MVC. Someone in Redmond decided that checkboxes should be truey/falsy (despite this being different to how checkboxes are designed to work in the HTML spec) so they put this hack workaround into MVC – Liam Jul 18 '18 at 12:32
  • Possible duplicate of [asp.net mvc: why is Html.CheckBox generating an additional hidden input](https://stackoverflow.com/questions/2697299/asp-net-mvc-why-is-html-checkbox-generating-an-additional-hidden-input) – Liam Jul 18 '18 at 12:33
  • I work around it by not using the Html helpers for checkboxes. – Liam Jul 18 '18 at 12:33

0 Answers0