[英]asp-for in checkbox throws and error in asp.net core
Here i have used two checkbox在这里我使用了两个复选框
<label asp-for="SendReport" class="checkbox-inline"><input type="checkbox" asp-for="SendReport" />Send Report</label><br>
<label asp-for="SendInvoice" class="checkbox-inline"><input type="checkbox" asp-for="SendInvoice" />Send Invoice</label><br>
and data type for both checboc ie asp-for="SendReport" and asp-for="SendInvoice"
is bit
. checboc 即
asp-for="SendReport" and asp-for="SendInvoice"
的数据类型是bit
。
Now the problem is that when ever i try to load the page its throws error as below现在的问题是,每当我尝试加载页面时,它都会抛出如下错误
Unexpected 'asp-for' expression result type 'System.Nullable`1[[System.Boolean, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' for <input>. 'asp-for' must be of type 'System.Boolean' or 'System.String' that can be parsed as a 'System.Boolean' if 'type' is 'checkbox'.<br>
Any help will be heartly thankful.任何帮助都将由衷感谢。 Thank you
谢谢
This is because asp-for
is always expecting a boolean parameter when input type is checkbox
, or a string parameter that can be parsed as boolean .这是因为当输入类型为
checkbox
, asp-for
总是需要一个布尔参数,或者一个可以解析为 boolean 的字符串参数。
This said, having the assumption that SendReport
and SendInvoice
are not boolean:这
SendReport
,假设SendReport
和SendInvoice
不是布尔值:
Unexpected 'asp-for' expression result type 'System.Nullable`
意外的“asp-for”表达式结果类型“System.Nullable”
you will have to change these parameters into boolean parameters that will be false by default, or into string parameters that will contain "false"
or "true"
by default.您必须将这些参数更改为默认情况下为 false 的布尔参数,或者更改为默认情况下包含
"false"
或"true"
的字符串参数。
I ran into the same issue when working with EFcore.我在使用 EFcore 时遇到了同样的问题。 From the database, I have:
从数据库中,我有:
namespace Project.EFCoreModels;
public partial class Account
{
public int AccountId {get; set;}
public bool? HasSaleTax {get; set;}
}
I want to display HasSalesTax
as a checkbox input with asp tag helper:我想将
HasSalesTax
显示为带有 asp 标签助手的复选框输入:
<input type="checkbox" asp-for="HasSaleTax" class="form-control" />
This thows the error because the the input type checkbox does not accept nullable value.这会引发错误,因为输入类型复选框不接受可为空值。 Fair enough.
很公平。
To work around it, I created another partial class, same namespace, but on a different project directory (folder).为了解决这个问题,我创建了另一个部分类,具有相同的命名空间,但位于不同的项目目录(文件夹)上。 This is a common pattern in .NetCore MVC when we want to add MetaData to an EFCore class.
当我们想要将 MetaData 添加到 EFCore 类时,这是 .NetCore MVC 中的常见模式。
namespace Project.EFCoreModels;
public partial class Account
{
public bool SalesTaxInput
{
get => this.HasSaleTax.GetValueOrDefault();
set {this.HasSaleTax = value;}
}
}
Then on the view, you just bind the input with the newly created prop:然后在视图上,您只需将输入与新创建的道具绑定:
<input type="checkbox" asp-for="SalesTaxInput" class="form-control" />
It is an issue with:
asp-for="SendReport"
.这是一个问题:
asp-for="SendReport"
。 That is not valid because the tag is input of type checkbox soasp-for
is expecting aboolean
and you are sending it a something else than a boolean.这是无效的,因为标签是复选框类型的输入,所以
asp-for
需要一个boolean
而您发送的不是布尔值。
Refer this:参考这个:
Asp.net MVC putting checkbox data in a list of booleans Asp.net MVC 将复选框数据放入布尔值列表中
When happened use bit datatype for reparent
true, false
but column allow null checked发生时使用位数据类型为 reparent
true, false
但列允许空检查
You can fixed by two way您可以通过两种方式修复
First : Set Not Allow null or remove ?
第一:设置不允许为空或删除
?
in filed在归档
Example:例子:
public bool? SendReport {get; set; }
// change to below code
public bool SendReport {get; set; }
Second: You can use normal html tag for checkbox input第二:您可以使用普通的html标签进行复选框输入
<input type="checkbox" id="SendReport" name="SendReport" @((Model.SendReport== false ? "" : "checked")) />
If you use a如果你使用一个
@Html.EditorFor(m => m.SendReport, new { @class = "form-control" }}
it will give you a dropdown box with options它会给你一个带有选项的下拉框
<select id="SendReport" name="SendReport" class="list-box tri-state">
<option value="">Not Set</option>
<option value="true">True</option>
<option value="false">False</option>
</select>
at least I do, when using Bootstrap 4 and Visual Studio 2019, with .NET Core 3.1至少我在使用 Bootstrap 4 和 Visual Studio 2019 时使用 .NET Core 3.1
Anyone running into this MVC core abandon the "for" @Html.CheckBox("tblSubCategory.Enabled",Model.tblSubCategory.Enabled.HasValue? Model.tblSubCategory.Enabled:false)任何遇到这个 MVC 核心的人都会放弃“for”@Html.CheckBox("tblSubCategory.Enabled",Model.tblSubCategory.Enabled.HasValue?Model.tblSubCategory.Enabled:false)
tblSubCategory is a sub model on a view, Enabled is a nullable boolean tblSubCategory 是视图上的子 model,Enabled 是可为空的 boolean
Just removing?只是删除? do not work.
不工作。 for me worked as i wrapped around a div claaa=checkbox
对我来说,当我围绕一个 div claaa=checkbox 工作时
<div class="form-group">
<label asp-for="IsDeleted" class="control-label"></label>
<div class="col-md-10">
<div class="checkbox">
<input asp-for="IsDeleted" class="form-check" />
<span asp-validation-for="IsDeleted" class="text-danger"></span>
</div>
</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.