Convert bool? to bool in View

时间:2021-02-16 02:56:31

i have checkbox in my which is like this @Html.CheckBoxFor(model => model.SKUs.Jewish) but my Jewish in database is nullable type so it gives me an error cannot implicitly convert type 'bool?' to 'bool'. how do check my model that it has values then it should show that else not.please help.

我在我的复选框是这样的@ Html.CheckBoxFor(model => model.SKUs.Jewish)但我的数据库中的犹太人是可以为空的类型所以它给我一个错误不能隐式转换类型'bool?' 'bool'。如何检查我的模型它有值,然后它应该显示其他not.please帮助。

3 个解决方案

#1


6  

You could use the following:

您可以使用以下内容:

@Html.CheckBox("SKUs.Jewish", Model.SKUs.Jewish.GetValueOrDefault());

If the value is not set it uses the nullable types default value which is false.

如果未设置该值,则使用可空类型的默认值false。

#2


0  

With a nullable type you check if it has a value with thing.HasValue, and get at the actual value using thing.Value.

使用可空类型,检查它是否具有thing.HasValue的值,并使用thing.Value获取实际值。

#3


0  

And finally i solve that problem. If you are using a checkbox; uncheck Allow Nulls option of your field in your DB. That's it!

最后我解决了这个问题。如果您使用复选框;取消选中数据库中字段的“允许空值”选项。而已!

#1


6  

You could use the following:

您可以使用以下内容:

@Html.CheckBox("SKUs.Jewish", Model.SKUs.Jewish.GetValueOrDefault());

If the value is not set it uses the nullable types default value which is false.

如果未设置该值,则使用可空类型的默认值false。

#2


0  

With a nullable type you check if it has a value with thing.HasValue, and get at the actual value using thing.Value.

使用可空类型,检查它是否具有thing.HasValue的值,并使用thing.Value获取实际值。

#3


0  

And finally i solve that problem. If you are using a checkbox; uncheck Allow Nulls option of your field in your DB. That's it!

最后我解决了这个问题。如果您使用复选框;取消选中数据库中字段的“允许空值”选项。而已!