简体   繁体   English

RadiobuttonFor和可为null的布尔检查属性问题

[英]RadiobuttonFor and nullable bool checked property issue

I have a MVC View Model in which I have nullable boolean property: 我有一个MVC视图模型,其中具有可为空的布尔属性:

[DisplayName("Is Active")]
public bool? IsActive { get; set; }

View: 视图:

<div class="col-md-3">
    <div class="row">
        <label>@Html.LabelFor(model => model.IsActive)</label>
    </div>
    @Html.RadioButtonFor(model => model.IsActive, true, new {id = "IsIsActiveTrue" })
    <label>Yes</label>
    @Html.RadioButtonFor(model => model.IsActive, false, new {id = "IsIsActiveFalse" })
    <label>No</label>
    @Html.RadioButtonFor(model => model.IsActive, "", new {id = "IsIsActiveAll"})
    <label>All</label>
</div>

By default, I would like to select All when page loads. 默认情况下,我想在页面加载时选择全部。 I tried to add 我试图添加

@checked = "checked" @checked =“已检查”

but it didn't work (any of radiobutton value is selected). 但它不起作用(选择了任何单选按钮值)。 Please advise. 请指教。

You cannot pass NULL as the value for the radio button when using RadioButtonFor helper method. 使用RadioButtonFor helper方法时,不能将NULL作为单选按钮的值传递。 If you pass "" as the value, you cannot set that in your GET action as your variable type is bool? 如果传递""作为值,则不能在GET操作中设置该值,因为变量类型为bool? ,not string. ,而不是字符串。

If you absolutely want 3 radiobuttons with your current type, you may simply write pure HTML code to render that. 如果您绝对希望使用当前类型的3个单选按钮,则可以只编写纯HTML代码来呈现它。 As long as your radio button's name attribute value matches your view model property name, model binding will work fine when you submit the form. 只要您的单选按钮的name属性值与视图模型属性名称匹配,则在提交表单时,模型绑定将正常工作。

@using (Html.BeginForm("Search","Customer"))
{
    <label>Active</label>
    <input type="radio" name="IsActive" value="true"/>

    <label>In Active</label>
    <input type="radio" name="IsActive" value="false"/>

    <label>All</label>
    <input type="radio" name="IsActive" checked="checked" />

    <input type="submit"/>
}

Here we are setting the checked attribute for all in the HTML markup. 在这里,我们为HTML标记中的所有属性设置了checked属性。

Another option is to switch your controls from 3 radio buttons to 2 checkboxes. 另一个选择是将控件从3个单选按钮切换到2个复选框。 You can mark all of them checked initially and user may select/unselect as needed. 您可以将所有标记都标记为最初已选中,并且用户可以根据需要选择/取消选择。 Another option is changing your type from bool? 另一个选择是从bool?更改您的类型bool? to an enum with these 3 values. 到具有这三个值的枚举。 With both of these approaches, with the use of CheckBoxFor or RadioButtonFor helper, you should be able to set the items which needs to be checked, in your GET action method. 通过这两种方法,通过使用CheckBoxForRadioButtonFor助手,您应该能够在GET操作方法中设置需要检查的项目。 Use the one which is more appropriate to your code structure/style. 使用一种更适合您的代码结构/样式的代码。

I did it by replacing RadiobuttonFor with RadioButton: 我通过用RadioButton替换RadiobuttonFor来做到这一点:

        <div class="row">
            <label>@Html.LabelFor(model => model.IsActive)</label>
        </div>
        <div class="row col-md-12">
            @Html.RadioButton("IsActive", true, false) Yes
            @Html.RadioButton("IsActive", false, false) No
            @Html.RadioButton("IsActive", null, true) All
        </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM