My Views/Shared/EditorTemplates/Boolean.cshtml 是双向的,处理可为空的和常规的复选框。
@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)
}
}
}
只有 2-3 个选择项的下拉列表对用户来说是不体贴的;它们需要单击两次,而单选按钮只需要单击一次(除非您没有房地产)。
当您的客户/用户尚未做出决定、不想做出决定、认为您不知道答案与您的业务有关,或者需要取消设置 true/1/on/positive 或 false/0/off/negative 时,因为两者都没有是准确的,Nullable<boolean> 的第三个选项可以表示待定 (tbd)、未设置、未知或不适用 (n/a)。