DataAnnotations: DisplayFormat not being honored
本文关键字:being honored not DisplayFormat DataAnnotations | 更新日期: 2023-09-27 18:16:19
这两种方法我都试过了:
[Display(Name = "Contract Value")]
[DisplayFormat(DataFormatString = "{0.##}")]
public decimal ContractValue { get; set; }
:
[Display(Name = "Contract Value")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0.##}")]
public decimal ContractValue { get; set; }
视图:
@Html.LabelFor(x => contractInfoModel.ContractValue)
@Html.TextBoxFor(x => contractInfoModel.ContractValue, new { maxlength = "100" })
如果我这样做,它工作。但是我不想这样做:
<input type="text" name="@Html.NameFor(x=>contractInfoModel.ContractValue)" id="@Html.IdFor(x=>contractInfoModel.ContractValue)" value="@Model.ContractInfoM.ContractValue.ToString("0.##")" class="numeric" maxlength="10" />
显示名称工作得很好。但它仍然显示了小数点后四位。我做错了什么?
谢谢!
找到了:
根据设计,在TextBoxFor中不支持DisplayFormat。所以不用这个:
@Html.TextBoxFor(x => contractInfoModel.ContractValue, new { maxlength = "10" })
我这样做了:
@Html.EditorFor(x => contractInfoModel.ContractValue, new { maxlength = "10" })
效果很好