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

标签 c# asp.net-core-2.0 razor-pages

这里我使用了两个复选框

<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>

checboc 的数据类型,即 asp-for="SendReport"和 asp-for="SendInvoice"bit
现在的问题是,每当我尝试加载页面时,它都会抛出如下错误

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>


任何帮助都将由衷感谢。谢谢

最佳答案

这是因为 asp-for 在输入类型为 checkbox总是需要一个 bool 参数,< strong>或可以解析为 bool 值的字符串参数。

这就是说,假设 SendReportSendInvoice 不是 bool 值:

Unexpected 'asp-for' expression result type 'System.Nullable`

您必须将这些参数更改为默认为 false 的 bool 参数,或者更改为默认包含 "false""true" 的字符串参数.

关于c# - 复选框中的 asp-for 抛出和 asp.net 核心中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53078044/

相关文章:

c# - MVC windows 身份验证在项目属性中变灰(变灰)

c# - ASP.net Identity 如何访问数据库,这是我应该使用的方式吗?

azure-pipelines - 在VSTS上使用.NET Core 2.0.0 Preview 1构建应用程序

c# - 不退还所有 claim 和保单授权问题

asp.net-core - 如何在_Layout.cshtml共享Razor页面中添加配置设置

c# - TextMode=数字无效

c# - .NET 中的堆栈与堆

c# - asp.net Core 2 具有复杂模型的自定义模型绑定(bind)器

c# - 分页和页面冲突 - ASP.Net Core Razor 页面与 MVC Controller 方法分页冲突

asp.net-core - asp.net core 中的 Url.Action