![](/img/trans.png)
[英]ASP.NET Core -> Label asp-for not returning label text
[英]asp-for in checkbox throws and error in asp.net core
在這里我使用了兩個復選框
<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" and 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>
任何幫助都將由衷感謝。 謝謝
這是因為當輸入類型為checkbox
, asp-for
總是需要一個布爾參數,或者一個可以解析為 boolean 的字符串參數。
這SendReport
,假設SendReport
和SendInvoice
不是布爾值:
意外的“asp-for”表達式結果類型“System.Nullable”
您必須將這些參數更改為默認情況下為 false 的布爾參數,或者更改為默認情況下包含"false"
或"true"
的字符串參數。
我在使用 EFcore 時遇到了同樣的問題。 從數據庫中,我有:
namespace Project.EFCoreModels;
public partial class Account
{
public int AccountId {get; set;}
public bool? HasSaleTax {get; set;}
}
我想將HasSalesTax
顯示為帶有 asp 標簽助手的復選框輸入:
<input type="checkbox" asp-for="HasSaleTax" class="form-control" />
這會引發錯誤,因為輸入類型復選框不接受可為空值。 很公平。
為了解決這個問題,我創建了另一個部分類,具有相同的命名空間,但位於不同的項目目錄(文件夾)上。 當我們想要將 MetaData 添加到 EFCore 類時,這是 .NetCore MVC 中的常見模式。
namespace Project.EFCoreModels;
public partial class Account
{
public bool SalesTaxInput
{
get => this.HasSaleTax.GetValueOrDefault();
set {this.HasSaleTax = value;}
}
}
然后在視圖上,您只需將輸入與新創建的道具綁定:
<input type="checkbox" asp-for="SalesTaxInput" class="form-control" />
這是一個問題:
asp-for="SendReport"
。 這是無效的,因為標簽是復選框類型的輸入,所以asp-for
需要一個boolean
而您發送的不是布爾值。
發生時使用位數據類型為 reparent
true, false
但列允許空檢查
您可以通過兩種方式修復
第一:設置不允許為空或刪除?
在歸檔
例子:
public bool? SendReport {get; set; }
// change to below code
public bool SendReport {get; set; }
第二:您可以使用普通的html標簽進行復選框輸入
<input type="checkbox" id="SendReport" name="SendReport" @((Model.SendReport== false ? "" : "checked")) />
如果你使用一個
@Html.EditorFor(m => m.SendReport, new { @class = "form-control" }}
它會給你一個帶有選項的下拉框
<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>
至少我在使用 Bootstrap 4 和 Visual Studio 2019 時使用 .NET Core 3.1
任何遇到這個 MVC 核心的人都會放棄“for”@Html.CheckBox("tblSubCategory.Enabled",Model.tblSubCategory.Enabled.HasValue?Model.tblSubCategory.Enabled:false)
tblSubCategory 是視圖上的子 model,Enabled 是可為空的 boolean
只是刪除? 不工作。 對我來說,當我圍繞一個 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.