我已经将POCO与“费用”作为浮动对象。
public class CostChart
{
public string itemType { get; set; }
public float? Cost{ get; set; }
}
我需要以货币格式返回Cost,例如
U $ 4,882.50。
我应该使用什么数据注释?
这就是我在视图中显示的方式。
<td>@Model.Cost</td>
您是否尝试过使用DataType.Currency
:
public class CostChart
{
public string itemType { get; set; }
[DataType(DataType.Currency)]
public float? Cost{ get; set; }
}
另外,您可以这样使用DataFormatString
:
[DisplayFormat(DataFormatString = "{0:C0}")]`
public float? Cost{ get; set; }
但是我更喜欢使用设置显示格式EditorFor
。这是有关扩展ASP.NET MVC的编辑器模板教程的出色教程。
这样,您只需在一个地方编写货币的显示逻辑,就不需要每次想显示货币金额时都添加该提取注释。
- 编辑
为了使其在EditorFor工作,你也可以添加ApplyFormatInEditMode = true
到年底DataFormatString
使路线为:
[DisplayFormat(DataFormatString = "{0:C0}", ApplyFormatInEditMode = true)]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句