本文介绍了为什么CheckBoxFor生产运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该复选框被连接到该字段为空。

在我看来,我得到以下错误:

<%= 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生产运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:13
查看更多