我有一個輸入欄位,用戶可以在其中輸入價格。我希望有一個0.00.
占位符僅在我洗掉模型系結時才有效: asp-for="Postage"
為什么會這樣?以及如何顯示占位符?
HTML 輸入欄位:
@model Website.Models.Game
<div class="form-group">
<label asp-for="Postage"></label>
<div class="input-icon">
<input type="number" class="form-control postageInput" placeholder="0.00" asp-for="Postage"/>
<i>£</i>
</div>
</div>
該模型:
public class Game {
[Key]
public int Id {get; set;}
[Required(ErrorMessage = "Enter postage amount or click on 'collection only'")]
public decimal Postage {get; set;}
[other properties here below...]
}
uj5u.com熱心網友回復:
當輸入的值為空時顯示占位符。由于小數總是有一個默認值,所以它不能為空。將Postage模型上的屬性型別更改為可為空的小數將解決此問題。
試試下面的代碼看看區別。
HTML:
<div class="form-group">
<label asp-for="Postage"></label>
<div class="input-icon">
<input type="number" class="form-control postageInput" placeholder="0.00" asp-for="Postage" />
<i>£</i>
</div>
</div>
<div class="form-group">
<label asp-for="PostageNullable"></label>
<div class="input-icon">
<input type="number" class="form-control postageInput" placeholder="0.00" asp-for="PostageNullable" />
<i>£</i>
</div>
</div>
模型:
public decimal Postage { get; set; }
public decimal? PostageNullable { get; set; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/343446.html
