DataAnnotations:DisplayFormat 不被尊重

DataAnnotations: DisplayFormat not being honored

这两个我都试过了:

[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" />

显示名称效果很好。但它仍然显示 4 位小数。我做错了什么?

谢谢!

找到了:

根据设计,DisplayFormat 在 TextBoxFor 中不受支持。所以不是这个:

@Html.TextBoxFor(x => contractInfoModel.ContractValue, new { maxlength = "10" })

我这样做了:

@Html.EditorFor(x => contractInfoModel.ContractValue, new { maxlength = "10" })

而且效果很好。