MVC验证 - Nullable Bool - 通过radiobuttons强制进行真/假选择

时间:2016-08-04 09:11:41

标签: c# asp.net-mvc

在MVC应用程序中

如果一个字段是可以为空的bool类型,则表示为radiobutton集

如何通过设置数据属性来强制执行客户端验证

正在使用客户端验证(jquery unobtrusive)

按照目前的情况,我将模板化的可自然bool作为单选按钮呈现给用户。有3个单选按钮,一个用于True,一个用于False,一个用于null。

隐藏了null单选按钮,因此强制用户从true或false选项中选择(null不是有效选项,null只表示用户尚未进行选择 - true或false必须被选中)

我尝试过以下其他地方的答案

[Range(typeof(bool), "false", "true", ErrorMessage = "You must make a selection")]

但这永远不会通过验证

我也试过了

[Required]

但这并不会强迫用户进行选择

更多信息: -

当布尔出现问题时?以无线电按钮表示。 如果使用bool的MVC默认模板? (这是一个包含3个选项的下拉列表,"未设置"," True"," False"然后验证按预期工作。

当作为无线电按钮显示时,单选按钮功能正常并将正确的状态保存到数据库,但验证不起作用。

示例代码

型号: -

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>
}

以上工作正常,没有为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>

问题与未为单选按钮创建的客户端验证数据属性有关

2 个答案:

答案 0 :(得分:2)

您可以为此目的编写小属性:

public class TrueFalseBoolAttribute: ValidationAttribute
{
    public override bool IsValid(Object value)
    {
        return value is bool;
    }
}

这对你有用。但我也认为如果你想有3个选项,最好的方法就是Enum。

答案 1 :(得分:0)

型号:

public bool? boolObj { get; set; }
public int intObj { get; set; }

查看:

@Html.TextBoxFor(p => p.boolObj )
@Html.TextBoxFor(p => p.intObj )

您可以从视图来源中看到页面上发生了什么:

<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 attribute

当然,在一个复选框上,这一切都非常多余,因为它只能被检查(真实)或不被检查(错误),因此所需的标签没有多大用处。

相关问题