提交时允许 ASP.NET MVC 表单中的 Nullable<bool>
Allow Nullable<bool> in ASP.NET MVC form on submission
我有一个模型,其中一些属性的类型为 Nullable。
public Nullable<bool> Continuing { get; set; }
public Nullable<bool> Incomplete { get; set; }
//..etc other properties omitted for brevity
我已经成功地将它绑定到一个带有 3 个单选按钮的表单中,但是当我提交表单时,ModelState.IsValid
returns 错误。
控制器:
[HttpPost]
public ActionResult CreateSub(MyModel model)
{
var errors = ModelState
.Where(x => x.Value.Errors.Count > 0)
.Select(x => new { x.Key, x.Value.Errors })
.ToArray();
我查看了错误,它说值 'null'
不允许用于类型 Nullable<bool>
的属性 - 我似乎不明白为什么。
错误:
编辑:
这就是我将属性绑定到收音机的方式:
<div class="btn-group" data-toggle="buttons">
@Html.RadioButtonFor(x => x.Continuing, true, new {@class = "Continuing-true", style="visibility: hidden; margin-left:-13px"})
<label class="btn-cohort btn btn-sm btn-default @if(Model.Continuing.HasValue && Model.Continuing.Value) { <text>btn-custom-green active</text> } " data-value="true">
<input type="radio" name="options" value="true" />
T
</label>
@if(!Model.Continuing.HasValue) {
@Html.RadioButtonFor(x => x.Continuing, "null", new {@checked = "checked", @class = "Continuing-null", style="visibility: hidden; margin-left:-13px"})
} else {
@Html.RadioButtonFor(x => x.Continuing, "null", new {@class = "Continuing-null", style="visibility: hidden; margin-left:-13px"})
}
<label class="btn-cohort btn btn-sm btn-default btn-n @if(!Model.Continuing.HasValue) { <text>active</text> } " data-value="null">
<input type="radio" name="options" value="null"/>
N
</label>
@Html.RadioButtonFor(x => x.Continuing, false, new {@class = "Continuing-false", style="visibility: hidden; margin-left:-13px"})
<label class="btn-cohort btn btn-sm btn-default @if(Model.Continuing.HasValue && !Model.Continuing.Value) { <text>btn-custom-red active</text> } " data-value="false">
<input type="radio" name="options" value="false" />
F
</label>
</div>
将您的 Html 助手用法更改为
@Html.RadioButtonFor(x => x.Continuing, "", ...)
和手册html到
<input ... value="" />
所以他们回发空字符串,而不是 string
的值 "null"
,ValueProviders
无法转换为 null
我有一个模型,其中一些属性的类型为 Nullable。
public Nullable<bool> Continuing { get; set; }
public Nullable<bool> Incomplete { get; set; }
//..etc other properties omitted for brevity
我已经成功地将它绑定到一个带有 3 个单选按钮的表单中,但是当我提交表单时,ModelState.IsValid
returns 错误。
控制器:
[HttpPost]
public ActionResult CreateSub(MyModel model)
{
var errors = ModelState
.Where(x => x.Value.Errors.Count > 0)
.Select(x => new { x.Key, x.Value.Errors })
.ToArray();
我查看了错误,它说值 'null'
不允许用于类型 Nullable<bool>
的属性 - 我似乎不明白为什么。
错误:
编辑:
这就是我将属性绑定到收音机的方式:
<div class="btn-group" data-toggle="buttons">
@Html.RadioButtonFor(x => x.Continuing, true, new {@class = "Continuing-true", style="visibility: hidden; margin-left:-13px"})
<label class="btn-cohort btn btn-sm btn-default @if(Model.Continuing.HasValue && Model.Continuing.Value) { <text>btn-custom-green active</text> } " data-value="true">
<input type="radio" name="options" value="true" />
T
</label>
@if(!Model.Continuing.HasValue) {
@Html.RadioButtonFor(x => x.Continuing, "null", new {@checked = "checked", @class = "Continuing-null", style="visibility: hidden; margin-left:-13px"})
} else {
@Html.RadioButtonFor(x => x.Continuing, "null", new {@class = "Continuing-null", style="visibility: hidden; margin-left:-13px"})
}
<label class="btn-cohort btn btn-sm btn-default btn-n @if(!Model.Continuing.HasValue) { <text>active</text> } " data-value="null">
<input type="radio" name="options" value="null"/>
N
</label>
@Html.RadioButtonFor(x => x.Continuing, false, new {@class = "Continuing-false", style="visibility: hidden; margin-left:-13px"})
<label class="btn-cohort btn btn-sm btn-default @if(Model.Continuing.HasValue && !Model.Continuing.Value) { <text>btn-custom-red active</text> } " data-value="false">
<input type="radio" name="options" value="false" />
F
</label>
</div>
将您的 Html 助手用法更改为
@Html.RadioButtonFor(x => x.Continuing, "", ...)
和手册html到
<input ... value="" />
所以他们回发空字符串,而不是 string
的值 "null"
,ValueProviders
无法转换为 null