My Views / Shared / EditorTemplates / Boolean.cshtml works both ways, handling empty and regular checkboxes.
@model bool? @{ if (ViewData.ModelMetadata.IsNullableValueType) { <text><div class="RB"></text> Dictionary<string, object> yesAttrs = new Dictionary<string, object>(); Dictionary<string, object> noAttrs = new Dictionary<string, object>(); Dictionary<string, object> nullAttrs = new Dictionary<string, object>(); yesAttrs.Add("id", ViewData.TemplateInfo.GetFullHtmlFieldId("") + "Yes"); noAttrs.Add("id", ViewData.TemplateInfo.GetFullHtmlFieldId("") + "No"); nullAttrs.Add("id", ViewData.TemplateInfo.GetFullHtmlFieldId("") + "NA"); if (Model.HasValue && Model.Value) { yesAttrs.Add("checked", "checked"); } else if (Model.HasValue && !Model.Value) { noAttrs.Add("checked", "checked"); } else { nullAttrs.Add("checked", "checked"); } @Html.RadioButtonFor(x => x, "true", yesAttrs) <label for="@(ViewData.TemplateInfo.GetFullHtmlFieldId(""))Yes">Yes</label> @Html.RadioButtonFor(x => x, "false", noAttrs) <label for="@(ViewData.TemplateInfo.GetFullHtmlFieldId(""))No">No</label> @Html.RadioButtonFor(x => x, "", nullAttrs) <label for="@(ViewData.TemplateInfo.GetFullHtmlFieldId(""))NA" class="nostrong" title="Unknown or To Be Determined">tbd</label> @:</div> } else { ModelState state = ViewData.ModelState[ViewData.ModelMetadata.PropertyName]; bool value = Model ?? false; if (state != null && state.Errors.Count > 0) { <div class="input-validation-error" style="float: left"></div> } else { @Html.CheckBox("", value) } } }
Drop-down lists for only 2-3 selected items are not relevant to users; they require two clicks, while the switches require only one click (if you do not have real estate).
When your customers / users haven’t made a decision, don’t want to make a decision, don’t think that you know that the answer is your business, or you need to reset true/1/on/positive or false/0/off/negative since none of them is not accurate, the third parameter for Nullable<boolean> may be "to be determined" (tbd), not specified, unknown or not applicable (n / a).
Jules bartow
source share