C# MVC验证-可空布尔-通过单选按钮强制选择真/假
在MVC应用程序中 其中字段为可为空的布尔类型,并显示为单选按钮集 如何通过设置数据属性来实施客户端验证 正在使用客户端验证(jquery不引人注目) 目前,我已经将可为空的bool模板化,以单选按钮的形式呈现给用户。有3个单选按钮,一个为真,一个为假,一个为空 null单选按钮是隐藏的,因此用户必须从true或false选项中进行选择(null不是有效选项,null只是表示用户尚未进行选择-必须选择true或false) 我试过以下方法,我在别处找到了相同问题的答案C# MVC验证-可空布尔-通过单选按钮强制选择真/假,c#,asp.net-mvc,C#,Asp.net Mvc,在MVC应用程序中 其中字段为可为空的布尔类型,并显示为单选按钮集 如何通过设置数据属性来实施客户端验证 正在使用客户端验证(jquery不引人注目) 目前,我已经将可为空的bool模板化,以单选按钮的形式呈现给用户。有3个单选按钮,一个为真,一个为假,一个为空 null单选按钮是隐藏的,因此用户必须从true或false选项中进行选择(null不是有效选项,null只是表示用户尚未进行选择-必须选择true或false) 我试过以下方法,我在别处找到了相同问题的答案 [Range(typeof
[Range(typeof(bool), "false", "true", ErrorMessage = "You must make a selection")]
但这从未通过验证
我也试过了
[Required]
但这并不强制用户进行选择
进一步资料:-
问题似乎存在时,布尔?显示为单选按钮。
如果对bool使用MVC默认模板?(这是一个下拉列表,有3个选项,“未设置”、“为真”、“为假”,然后验证按预期工作
当显示为单选按钮时,单选按钮起作用并将正确的状态保存到数据库中,但验证不起作用
示例代码
型号:-
public class SomeModel
{
[Required]
public string Name { get; set; }
[Required]
public bool? SomeBool { get; set; }
}
视图:-
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.SomeBool)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.SomeBool)
@Html.ValidationMessageFor(model => model.SomeBool)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true)
@LabelFor(model=>model.Name)
@EditorFor(model=>model.Name)
@Html.ValidationMessageFor(model=>model.Name)
@LabelFor(model=>model.SomeBool)
@EditorFor(model=>model.SomeBool)
@Html.ValidationMessageFor(model=>model.SomeBool)
}
上面的工作原理与预期一样,没有为bool设置编辑器模板,但没有为单选按钮设置编辑器模板
单选按钮模板:-
@model bool?
<div>
@{
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>
<span style="display: none">
@Html.RadioButtonFor(x => x, "null", nullAttrs) <label for="@(ViewData.TemplateInfo.GetFullHtmlFieldId(""))NA">N/A</label>
</span>
</div>
@modelbool?
@{
Dictionary yesAttrs=新字典();
字典noAttrs=新字典();
Dictionary nullAttrs=新字典();
yesAttrs.Add(“id”,ViewData.TemplateInfo.getFullHtmlFieldDid(“是”);
noAttrs.Add(“id”,ViewData.TemplateInfo.getFullHtmlFieldDid(“”+“否”);
nullAttrs.Add(“id”,ViewData.TemplateInfo.getFullHtmlFieldDid(“”+“NA”);
if(Model.HasValue&&Model.Value)
{
添加(“已检查”、“已检查”);
}
else if(Model.HasValue&&!Model.Value)
{
添加(“已检查”、“已检查”);
}
其他的
{
添加(“已检查”、“已检查”);
}
}
@RadioButton(x=>x,“true”,yesAttrs)是
@RadioButton(x=>x,“false”,noAttrs)否
@Html.RadioButton(x=>x,“空”,空属性)不适用
问题将与没有为单选按钮创建客户端验证数据属性有关您可以为此编写小属性:
public class TrueFalseBoolAttribute: ValidationAttribute
{
public override bool IsValid(Object value)
{
return value is bool;
}
}
这应该适合您。但我也认为,如果您想要3个选项,最好的方法是枚举。模型:
public bool? boolObj { get; set; }
public int intObj { get; set; }
视图:
您可以从视图源查看页面上发生的情况:
<input data-val="true" data-val-required="The boolObj field is required." id="boolObj " name="boolObj " type="text" value="False">
<input data-val="true" data-val-number="The field intObj must be a number." data-val-required="The intObj field is required." id="intObj " name="intObj " type="text" value="0">
可为空的bool没有验证属性,而bool有一个data val required
标记。int有一个data val required
标记和一个data val number属性
当然,在复选框上,这是非常多余的,因为它只能被选中(true)或不被选中(false),所以所需的标记没有多大用处。只需添加
[必需]
属性-它将要求用户选择true
或false
。如果它不适用于您,则说明您没有显示的代码存在其他问题。bool应该只有2个值,true或false。如果您想要3个值,则可能使用枚举或字符串。我个人认为bool对于以下情况是错误的数据类型:这就是我的观点。我不想要3个值。我只想要true或false。bool是可为空的,因此false和用户尚未选择任何内容之间存在差异。用户必须仅选择true或false。如果不可为空,false将是默认值,因此不可能强制用户选择s选择你到底为什么要有这个模板-你只需要3行代码@Html.radiobutton(x=>x,true,new{id=”“})
,@Html.radiobutton(x=>x,false,new{id=”“})
和@Html.radiobutton(x=>x,”,new{id=”“})
注意最后一个单选按钮有一个null
值,而不是字符串“null”
(它不是null
),然后将它们包装在
元素中
<input data-val="true" data-val-required="The boolObj field is required." id="boolObj " name="boolObj " type="text" value="False">
<input data-val="true" data-val-number="The field intObj must be a number." data-val-required="The intObj field is required." id="intObj " name="intObj " type="text" value="0">