可空布尔值列表始终归零 [英] list of nullable bool values is always nulls

查看:185
本文介绍了可空布尔值列表始终归零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个局部视图。在我的部分的一些属性是可空布尔值。如果我选中或清除该复选框它总是空当我保存了这个纪录。我应该怎么做,如果我希望它返回false时检查取消和真正取消选中时检查,否则空?

下面是实体类:

 公共字符串描述{搞定;组; }
    [需要]
    公共字符串的DocType {搞定;组; }
    [显示(名称=用户)]
    [需要]
    公众诠释键{搞定;组; }
    [显示(NAME =路径)
    公共字符串名称{;组; }
    公共BOOL? ShowFullWidth {搞定;组; }
    公开名单< D​​mFormDefDTO> DmFormDefItems {搞定;组; }
    公共BOOL? IsEnabled {搞定;组; }

下面是我的部分观点:

 如果(Model.DmFormDefItems!= NULL)
        {
< D​​IV CLASS =主体>
    < D​​IV ID =容器体>
        <表类=表表边框的表格条纹ID =ordertable>
            <&THEAD GT;
                &所述; TR>                    <第i个已启用
                    < /第i
                < / TR>
            < / THEAD>
            <&TBODY GT;
                @for(INT I = 0; I< Model.DmFormDefItems.Count;我++)
                {
                    &所述; TR类=OrderDetailRow>
                        &所述; TD>
                            @if(Model.DmFormDefItems.ElementAt(I).ShowFullWidth == true)而
                            {
                                @ Html.CheckBoxFor(M = GT; m.DmFormDefItems [I] .ShowFullWidth.HasValue)LT;标签>查看全宽页< /标签>
                            }
                            其他
                            {
                                @ Html.CheckBoxFor(M = GT; m.DmFormDefItems [I] .ShowFullWidth.HasValue)LT;标签>查看全宽页< /标签>
                            }
                        < / TD>
                        &所述; TD>
                            @if(Model.DmFormDefItems.ElementAt(I).IsEnabled == true)而
                            {
                                @ Html.CheckBoxFor(M = GT; m.DmFormDefItems [I] .IsEnabled.HasValue)
                            }
                            其他
                            {
                                @ Html.CheckBoxFor(M = GT; m.DmFormDefItems [I] .IsEnabled.HasValue)
                            }
                        < / TD>
                    < / TR>
                }
            < / TBODY>
        < /表>


解决方案

首先,你不绑定到你的布尔属性,您绑定到这是一个只读属性的的HasValue 属性的可空< T> 参考文档),这意味着,当你回来后不能设置,因此在控制器的价值永远是默认的值。

一个复选框,仅有2个国家(真正),但可空<布尔> 有3种状态(真正),所以没有办法一个可空<布尔方式> 可重新$使用复选框,输入p $ psented

3可能的选择。


  1. 使用 @ Html.EditorFor(M = GT; m.yourNullableBool)这将产生一个
    下拉3的值(真,假和未设置)

  2. 使用3单选势必按钮你的可空<布尔> 属性与价值
    真正

  3. 发展你自己的HTML帮助和jQuery插件

I have a partial view. In my partial a few properties are nullable boolean. If I check or uncheck the checkbox it is always null when I save the this record. What should I do if I want it to return false when check to uncheck and true when uncheck to check, otherwise null?

Here is the Entity class:

    public string Description { get; set; }
    [Required]
    public string DocType { get; set; }
    [Display(Name = "User")]
    [Required]
    public int Key { get; set; }
    [Display(Name = "Path")]
    public string Name { get; set; }
    public bool? ShowFullWidth { get; set; }
    public List<DmFormDefDTO> DmFormDefItems { get; set; }
    public bool? IsEnabled { get; set; }

Here is my partial view:

  if (Model.DmFormDefItems != null)
        {
<div class="main-body">
    <div id="container-body">
        <table class="table table-bordered table-striped" id="ordertable">
            <thead>
                <tr>

                    <th>Enabled
                    </th>
                </tr>
            </thead>
            <tbody>
                @for (int i = 0; i < Model.DmFormDefItems.Count; i++)
                {
                    <tr class="OrderDetailRow">
                        <td>
                            @if (Model.DmFormDefItems.ElementAt(i).ShowFullWidth == true)
                            {
                                @Html.CheckBoxFor(m => m.DmFormDefItems[i].ShowFullWidth.HasValue) <label>Show Full Width Page</label>    
                            }
                            else
                            {
                                @Html.CheckBoxFor(m => m.DmFormDefItems[i].ShowFullWidth.HasValue) <label>Show Full Width Page</label>                                            
                            }
                        </td>
                        <td>
                            @if (Model.DmFormDefItems.ElementAt(i).IsEnabled == true)
                            {
                                @Html.CheckBoxFor(m => m.DmFormDefItems[i].IsEnabled.HasValue)     
                            }
                            else
                            {
                                @Html.CheckBoxFor(m => m.DmFormDefItems[i].IsEnabled.HasValue)        
                            }
                        </td>
                    </tr>              
                }
            </tbody>
        </table>

解决方案

Firstly you not binding to your boolean property, your binding to its HasValue property which is a readonly property of Nullable<T> (refer documentation) which means it cannot be set when you post back, so the value in your controller will always be the default null value.

A checkbox only has 2 states (true or false) but a Nullable<bool> has 3 states (true or false or null) so there is no way a Nullable<bool> can be represented using a checkbox input.

3 possible options

  1. Use @Html.EditorFor(m => m.yourNullableBool) which will generate a dropdown with 3 values ("True", "False" and "Not Set")
  2. Use 3 radio buttons bound to your Nullable<bool> property with the values true, false and null
  3. Develop you own html helper and jquery plugin

这篇关于可空布尔值列表始终归零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆