该复选框被连接到该字段为空。
在我看来,我得到以下错误:
无法隐式转换类型'布尔? 以布尔
<%= Html.CheckBoxFor(型号=> model.Product.Exclusive)%GT;我如何解决它,而无需更改数据库的设计?
解决方案独家不可能是空的,它评估前pression时,是没有意义的视图引擎。它要么检查或不选中复选框,并还带有真正或假价值做出回应。您的模型需要有一个布尔价值,但并不意味着你的数据库必须知道。你只需要做一个翻译的地方数据库和模型,例如之间。 Model.Exclusive = DAO.Exclusive?假。
不知道什么空RE presents数据中的模式,或者你如何生成的模型对象,很难给你比很多更详细。
编辑:没有测试过这一点,但你可能会逃脱简单的东西如
公共BOOL NoNullExclusive{ {返回独占?假; } 集合{独家=价值; }}和替换
<%= Html.CheckBoxFor(型号=> model.Product.Exclusive)%GT;与
<%= Html.CheckBoxFor(型号=> model.Product.NoNullExclusive)%GT;The field the CheckBox is wired to is nullable.
On my view I get the following error:
Cannot implicitly convert type 'bool?' to 'bool'
<%= Html.CheckBoxFor(model => model.Product.Exclusive) %>
How do I fix it without having to change the database design?
解决方案Exclusive cannot be Nullable, it makes no sense to the ViewEngine when evaluating the expression. It has to either check or not check the checkbox and also respond with a true or false value. Your model needs to have a bool value but that doesn't mean your database has to know that. You just have to do a translation somewhere between the database and the Model, eg. Model.Exclusive = DAO.Exclusive ?? false.
Without knowing what null represents in your data schema or how you generate your model objects, it's hard to give you a lot more detail than that.
Edit: haven't tested this but you might get away with something as simple as
public bool NoNullExclusive { get { return Exclusive ?? false; } set { Exclusive = value; } }and replacing
<%= Html.CheckBoxFor(model => model.Product.Exclusive) %>with
<%= Html.CheckBoxFor(model => model.Product.NoNullExclusive) %>
更多推荐
为什么CheckBoxFor生产运行时错误
发布评论