下面是如何在bullable布尔bool?
财产在DropDownListFor
地图:
@model SomeModel
<!-- ...some HTML... -->
@Html.DropDownListFor(m => m.NullableBooleanProperty, new SelectList(
new[] {
new { Value = "", Text = "-- Choose YES or NO --" },
new { Value = "true", Text = "YES" },
new { Value = "false", Text = "NO" },
},
"Value",
"Text"
))
下面是如何将其映射到CheckBoxFor
通过使用不可为空代理属性作为一种解决方法:
在视图模型:
public bool NullableBooleanPropertyProxy
{
get
{
return NullableBooleanProperty == true;
}
set
{
NullableBooleanProperty = value;
}
}
在查看:
@model SomeModel
<!-- ...some HTML... -->
@Html.CheckBoxFor(m => m.NullableBooleanPropertyProxy)
这种解决方法的唯一缺点是,null
值将被视为false
:如果你不能接受,最好使用控件可以支持三种状态,比如前面提到的DropDownListFor
。
欲了解更多信息,请致电read here。
为什么你需要检查模型是否已填充?也许有另一种方式,一种正确的方式,去做你需要做的事情? – AlexanderMP 2010-06-14 09:14:53
可能重复的[为什么CheckBoxFor产生运行时错误](http://stackoverflow.com/questions/2490790/why-is-checkboxfor-producing-runtime-error) – 2011-09-01 12:36:07