DataAnnotations:DisplayFormat不受尊重

时间:2016-11-04 20:12:37

标签: c# asp.net-mvc data-annotations

我试过这两个:

[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位小数。我做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

找到它:

按照设计,在TextBoxFor中不支持DisplayFormat。所以不要这样:

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

我这样做了:

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

效果很好。