我有以下POCO,成本为浮动.
- public class CostChart
- {
- public string itemType { get; set; }
- public float? Cost{ get; set; }
- }
我需要以货币格式返回成本,例如
$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来设置显示格式.这是一个关于Extending Editor Templates for ASP.NET MVC教程的很棒的教程.