Using @Html.Formblock for checkboxes not working
I'm liking the HTML Helpers, but ran into an issue in using the Html.FormBlock to render input controls for a boolean value. The code that was rendered worked fine when setting the value from true to false, but did not work when setting it from false to true.
Here are my tags and what they rendered when IsActive is true:
@Html.FormBlock(m => m.Org.IsActive)
rendered
<input type="checkbox" checked="true" name="Org.IsActive" id="Org_IsActive" value="True" data-val="true" data-val-required="The IsActive field is required." class="form-control">
<input type="checkbox" name="Org.IsActive" id="Org_IsActive" value="False" data-val="true" data-val-required="The IsActive field is required." class="form-control">
<div class="form-group">
<label asp-for="Org.IsActive" class="control-label"></label>
<input asp-for="Org.IsActive" class="form-control">
</div>
rendered:
<input class="form-control" type="checkbox" checked="checked" data-val="true" data-val-required="The IsActive field is required." id="Org_IsActive" name="Org.IsActive" value="true">
<input class="form-control" type="checkbox" data-val="true" data-val-required="The IsActive field is required." id="Org_IsActive" name="Org.IsActive" value="true">
The differences seem to be
- When IsActive is true, checked is set to "true" in the html helper and "checked" in the tag helper.
- When IsActive is false, value is set to "true" in the tag helper and "false" in the html helper.
I'm not sure how I can work around this issue without just going back to using tag helpers for boolean fields. Any ideas would be appreciated.
Thanks!
Laurie
What do you mean when you say "set it from true to false"? Like, checking/unchecking the box in the browser?
Yes, that's what I mean. As an clearer example, I created a field IsOnDeansList in the Student class and updated the Create Razor Page to add it. When I used the standard tag helpers, I was able to set the value of the field to true. When I used Html.FormBlock I was not.
I'm attaching the pages I updated to make the example work:
Well that is odd. Definitely a bug!