【问题标题】:How to format a decimal that hasn't been initialized in C# MVC?如何格式化尚未在 C# MVC 中初始化的小数?
【发布时间】:2015-09-16 12:35:12
【问题描述】:

我正在构建一个在字段中显示货币金额的表单。从视图上看,如果小数点没有设置,我希望它显示“0.00”

我该怎么做?

查看模型:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:0.00}")] //what I tried
public decimal MyDecimal { get; set; }

查看:

@Html.TextBoxFor(model => model.MyDecimal)

方框显示“0”而不是“0.00”

【问题讨论】:

    标签: c# asp.net-mvc formatting decimal


    【解决方案1】:

    DataFormatString = "{0:0.00}") 仅在您使用 @Html.EditorFor()@Html.DisplayFor() 时受到尊重

    如果使用TextBoxFor()你需要使用

    @Html.TextBoxFor(model => model.MyDecimal, "{0:0.00}")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      • 2022-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多