I'm new from WebForms to MVC. I have view model field with the type bool?
and, by default, EditorFor()
renders this field as a DropDownList with a "Not Set" option. I would prefer to render it as a CheckBox and, if the value is null, just set it to unchecked.
The field name is RFP.DatesFlexible
and so I wrote the following markup in my view:
<input type="checkbox" id="RFP_DatesFlexible" name="RFP.DatesFlexible" />
<label for="RFP_DatesFlexible">My Dates are Flexible</label>
But this doesn't work. The result is always null and ModelState.IsValid
is false.
Can anyone say how I could make this work?
EDIT
This is the code I ended up with, which appears to work fine.
@Html.CheckBox("RFP.DatesFlexible", Model.RFP.DatesFlexible ?? false)
@Html.Label("RFP.DatesFlexible", "My Dates are Flexible")
The label is correctly associated with the checkbox so that clicking the text will toggle the checkbox.