asp.net-mvc ASP.net MVC CheckBoxFor 转换错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2267975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:08:16  来源:igfitidea点击:

ASP.net MVC CheckBoxFor casting error

asp.net-mvccheckboxfor

提问by Jeremy Sullivan

I have a EF entity that is tied to a SQL table that contains a bit field called "Active". I generate the Edit code from the T4 template, and the page inherits from the EF entity. At the bottom of the page, it generated a CheckBoxFor like this:

我有一个与 SQL 表相关联的 EF 实体,该表包含一个名为“Active”的位字段。我从 T4 模板生成编辑代码,页面继承自 EF 实体。在页面底部,它生成了一个 CheckBoxFor,如下所示:

<%= Html.CheckBoxFor(model => model.Active) %>

I get the wonderful red squiggly under model.Active, and the error message says that I cannot implicitly convert type bool? to bool. So, I tried the following:

我在model.Active下看到了美妙的红色波浪线,错误消息说我不能隐式转换类型bool?布尔。所以,我尝试了以下方法:

<%= Html.CheckBoxFor(model => (bool)model.Active) %>

It, of course, didn't like that and gave me this error:

当然,它不喜欢那样并给了我这个错误:

System.InvalidOperationException: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

System.InvalidOperationException:模板只能用于字段访问、属性访问、单维数组索引或单参数自定义索引器表达式。

I'm probably missing something simple.

我可能错过了一些简单的东西。

回答by LukLed

Try model.Active.Value

尝试 model.Active.Value

If this field should be not nullable then you should change data type on database side to not null.

如果此字段不应为空,则应将数据库端的数据类型更改为not null.

EDIT

编辑

Why does this happen?

为什么会发生这种情况?

Your database field is defined as FIELDNAME BIT NULL. It means that it can contain three values: NULL, true and false. Since it can contain three values, it is mapped to bool? type in entity framework. bool? is another name of Nullable<bool>, which is wrapper around bool allowing it to have third value: NULL. Since CheckBoxFor expects one of two values - true or false, it can't except Nullable<bool>. Every Nullable has property called value which returns wrapped type. But you should be aware that when database field will contain null Nullable<bool>.Valuewill throw an error. If you are sure that this field should not contain NULL values, you should change it's data type to FIELDNAME BIT NOT NULLand generate model from database again. This will change data type from bool? to bool and there willbe no need to call Value property.

您的数据库字段定义为FIELDNAME BIT NULL. 这意味着它可以包含三个值:NULL、true 和 false。既然可以包含三个值,就映射到bool?输入实体框架。布尔?是 的另一个名称Nullable<bool>,它是 bool 的包装器,允许它具有第三个值:NULL。由于 CheckBoxFor 需要两个值之一 - true 或 false,所以它不能除了Nullable<bool>. 每个 Nullable 都有一个名为 value 的属性,它返回包装类型。但是您应该知道,当数据库字段包含 null 时Nullable<bool>.Value会引发错误。如果您确定此字段不应包含 NULL 值,则应将其数据类型更改为FIELDNAME BIT NOT NULL并再次从数据库生成模型。这会改变 bool 的数据类型吗?到 bool 并且不需要调用 Value 属性。