I have a column in my Model with a NULLABLE boolean value. Now on my View (for editing), I would like to bind that to two radiobuttons: Yes & No. If the value is null, then
I can across this post late, however here is my solution which handles the setting the values as well. This used an Editor template. You will need to specify the Editor template name. I called mine 'NullableBool'
@model bool?
@{
Layout = null;
IDictionary yesAttributes = new Dictionary();
IDictionary noAttributes = new Dictionary();
IDictionary naAttributes = new Dictionary();
if (Model.HasValue)
{
if (Model.Value)
{
yesAttributes.Add("checked", "checked");
}
else
{
noAttributes.Add("checked", "checked");
}
}
else
{
naAttributes.Add("checked", "checked");
}
}
@Html.RadioButtonFor(m => m, "true", yesAttributes) Yes`
@Html.RadioButtonFor(m => m, "false", noAttributes) No`
@Html.RadioButtonFor(m => m, string.Empty, naAttributes) N/A`