可空布尔值列表始终归零 [英] list of nullable bool values is always nulls
问题描述
我有一个局部视图。在我的部分的一些属性是可空布尔值。如果我选中或清除该复选框它总是空当我保存了这个纪录。我应该怎么做,如果我希望它返回false时检查取消和真正取消选中时检查,否则空?
下面是实体类:
公共字符串描述{搞定;组; }
[需要]
公共字符串的DocType {搞定;组; }
[显示(名称=用户)]
[需要]
公众诠释键{搞定;组; }
[显示(NAME =路径)
公共字符串名称{;组; }
公共BOOL? ShowFullWidth {搞定;组; }
公开名单< DmFormDefDTO> DmFormDefItems {搞定;组; }
公共BOOL? IsEnabled {搞定;组; }
下面是我的部分观点:
如果(Model.DmFormDefItems!= NULL)
{
< DIV CLASS =主体>
< DIV 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可能的选择。
- 使用
@ Html.EditorFor(M = GT; m.yourNullableBool)
这将产生一个
下拉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
- Use
@Html.EditorFor(m => m.yourNullableBool)
which will generate a dropdown with 3 values ("True", "False" and "Not Set") - Use 3 radio buttons bound to your
Nullable<bool>
property with the valuestrue
,false
andnull
- Develop you own html helper and jquery plugin
这篇关于可空布尔值列表始终归零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!