C# 允许为空<;布尔>;在ASP.NET MVC表单中提交

C# 允许为空<;布尔>;在ASP.NET MVC表单中提交,c#,forms,asp.net-mvc-4,modelstate,C#,Forms,Asp.net Mvc 4,Modelstate,我有一个模型,其中一些属性的类型为Nullable public Nullable<bool> Continuing { get; set; } public Nullable<bool> Incomplete { get; set; } //..etc other properties omitted for brevity 我查看了错误,它说类型Nullable的属性不允许使用值'null'——我似乎不明白为什么 错误: 编辑: 以下是我将属性绑定到收音机的方式

我有一个模型,其中一些属性的类型为Nullable

public Nullable<bool> Continuing { get; set; }
public Nullable<bool> Incomplete { get; set; }

//..etc other properties omitted for brevity
我查看了错误,它说类型
Nullable
的属性不允许使用值
'null'
——我似乎不明白为什么

错误:

编辑:

以下是我将属性绑定到收音机的方式:

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

@RadioButton(x=>x.continue,true,new{@class=“continue true”,style=“可见性:隐藏;左边距:-13px”})
T
@如果(!Model.continue.HasValue){
@RadioButton(x=>x.继续,“null”,新的{@checked=“checked”,@class=“continue null”,style=“可见性:隐藏;左边距:-13px”})
}否则{
@RadioButton(x=>x.continue,“null”,新的{@class=“continue null”,style=“可见性:隐藏;左边距:-13px”})
}
N
@RadioButton(x=>x.continue,false,new{@class=“continue false”,style=“可见性:隐藏;左边距:-13px”})
F

将Html助手的用法更改为

@Html.RadioButtonFor(x => x.Continuing, "", ...)
和手动html

<input ... value="" />


因此,它们将返回空字符串,而不是值为
字符串
“null”
值提供程序
无法将其转换为
null

显示如何在视图中生成单选按钮(最好的猜测是,您为单选按钮指定的值是
“null”
,而不是
)@StephenMuecke我已经编辑了我的帖子,我可以理解为什么在我的OP中包含它是有用的,但一开始我忽略了它,因为它有点混乱,因为我使用的是引导式收音机。这将在以后产生问题,因为当我将它保存到数据库时,我希望值实际为null,而不是空字符串。当它绑定时,它将是
null
ValueProviders
将空字符串转换为null,但不能将字符串
“null”
转换为
null
-记住您刚刚发布的键/值对-它的所有有效字符串我现在明白了,我认为问题是它不允许null,不是因为它不能将“null”转换为null。谢谢,您的代码中还有其他一些奇怪的东西,例如
@checked=“checked”
——这些东西将被完全忽略,因此有点毫无意义。使用强类型帮助程序的全部目的是绑定到您的模型属性,因此它是
continue
的值,它决定了所选择的内容(而不是
@checked=“checked”
)-谢天谢地,因为如果不这样做,模型绑定将全部失败:)是,这很奇怪,但这是我在提交表单并使用相同模型重新加载页面时遇到的一个问题的一个解决办法。由于某种原因,选择为true的值将返回为null。
<input ... value="" />