我有一个带有数据注释[DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
的类,根本不会格式化。我也将using System.ComponentModel.DataAnnotations;
包括在我的课程中。
下面是我的课:
public class StepTwo : IEnumerable
{
public int Id { get; set; }
public string Party { get; set; }
public string Currency { get; set; }
[DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
public double? Amount { get; set; }
public IEnumerator GetEnumerator()
{
return GetEnumerator();
}
}
现在当我在程序中使用此类时,数字是否未格式化?我知道我可以在Razor页面中执行此操作,但是使用数据批注的重点是您不必在任何地方都执行此操作。
我已经尝试过[DisplayFormat(ApplyFormatInEditMode = true), DataFormatString = "{0:n2}"]
,但它没有做任何更改。
我不确定为什么未格式化?我想念什么?
感谢您的帮助,不胜感激。
我已经包含以下与Razor有关的页面:
<table class="table table-hover">
@foreach (var cus in Model.SSTwo)
{
<tr>
<td>@cus.Id</td>
<td>@cus.Currency</td>
<td>@cus.Party</td>
<td>@cus.Amount</td>
</tr>
}
</table>
您已将ApplyFormatInEditMode设置为true,因此格式化仅在编辑模式下有效,只需将其删除即可在视图中查看格式化工作。
尝试一下:
<table class="table table-hover">
@foreach (var cus in Model.SSTwo)
{
<tr>
<td>@cus.Id</td>
<td>@cus.Currency</td>
<td>@cus.Party</td>
<td>@Html.DisplayFor(modelitem=>cus.Amount)</td>
</tr>
}
</table>