If you can't change the model property from
bool?
to
bool
, then you're going to have to use the
CheckBox
and
LabelFor
methods, rather than the
CheckBoxFor
method:
@Html.CheckBox(nameof(Model.IsSelected), Model.IsSelected ?? false)
@Html.LabelFor(m => m.IsSelected)
Alternatively, you could use
EditorFor
with a custom editor, as described in
this StackOverflow answer[
^].
Or, you could add a non-nullable wrapper property to your viewmodel, and use that with the
CheckBoxFor
method:
public bool IsSelectedNotNull
{
get { return IsSelected ?? false; }
set { IsSelected = value; }
}
@Html.CheckBoxFor(m => m.IsSelectedNotNull)