简体   繁体   English

复选框中的 asp-for 抛出和 asp.net 核心中的错误

[英]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 .这是因为当输入类型为checkboxasp-for总是需要一个布尔参数,或者一个可以解析为 boolean 的字符串参数

This said, having the assumption that SendReport and SendInvoice are not boolean:SendReport ,假设SendReportSendInvoice不是布尔值:

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 so asp-for is expecting a boolean 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.

相关问题 ASP.NET 核心 -&gt; Label asp-for 不返回 label 文本 - ASP.NET Core -> Label asp-for not returning label text 指定“asp-for”HTML标记的格式(ASP.NET Core) - Specify a format for “asp-for” HTML Tag (ASP.NET Core) 将 model 传递给 asp-for | ASP.net核心 - Passing model to asp-for | ASP.net Core ASP NET Core 中的 asp-for 发送复选框值和 false,即使复选框被选中 - asp-for in ASP NET Core send checkbox value and false even if checkbox is checked 通过 asp-for="..." 将字符串值传递到 ASP.Net 核心中的 Model 无法正常工作 - Passing a String value through asp-for=“…” into a Model in ASP.Net core isn't working the way it should ASP.NET Core MVC:asp-for 正在渲染不完整的 id 和 name 属性 - ASP.NET Core MVC: asp-for is rendering incomplete id and name attributes ASP.NET 核心 6,在 EditorTemplate 上下文中获取自定义 asp-for TagHelper 的元数据 - ASP.NET Core 6, Obtaining Metadata for a custom asp-for TagHelper in the context of an EditorTemplate 将值从“选择asp-for”传递到“ textarea” asp.net核心剃须刀 - Passing value from “select asp-for” to a “textarea” asp.net core razor 获取复选框名称 ASP.NET 核心 - Get checkbox names ASP.NET Core 当在剃刀页面 ASP.NET Core MVC 中与 asp-for 一起使用时,Textarea 不会在其中显示任何内容 - Textarea won't show anything inside of it when used with asp-for in razor-pages ASP.NET Core MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM