【问题标题】:DataFormatString = "{0:N}" not working on Model MVCDataFormatString = "{0:N}" 不适用于模型 MVC
【发布时间】:2020-10-10 10:14:24
【问题描述】:

我想要在 CurrentCulture 的函数中为数字类型“int”的千分位和逗号。我正在尝试使用 DataFormtString 但它不起作用。 例如:1234567 ==> 123.456.789

我正在使用 MVC,我想在表格、输入等中格式化数字。

.Replace() 或类似的东西不是解决方案,我希望它具有以下功能 当前文化。

例如:

    [DisplayName("Test")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:N}")] // Not working
    public int? Example{ get; set; }

在视图中:

        <span class="text">@Model.Example</span>
        <input asp-for="Example" class="form-control form-control-sm" />

【问题讨论】:

  • its not working - 会发生什么?
  • 什么都没有,只显示完整的数字,没有点数
  • 如果你不使用@Html.DisplayFor(),显然它是行不通的。
  • 当我尝试时。它得到 123.456.789,00 。看来您的问题已经解决了?
  • 使用 DisplayFor 没问题,但无论如何使用 TagHelper

标签: asp.net-mvc asp.net-core integer dataformat displayformat


【解决方案1】:

根据你的描述,我自己做了一个测试demo,发现值已经设置为输入了,但是因为输入类型的值,无法显示。

我建议您可以将输入类型设置为文本,然后再试一次。

更多细节,您可以参考以下代码:

<span class="text">@Model.Example</span>

<input asp-for="Example" class="form-control form-control-sm" type="text" />

结果:

【讨论】:

  • 感谢您的回复!类型必须隐藏,不能以文本开头。在另一个功能之后,将显示此输入
  • 我不明白为什么要隐藏您的类型。如果要隐藏输入,可以将 css 显示设置为无。
猜你喜欢
  • 1970-01-01
  • 2013-03-15
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-13
  • 1970-01-01
相关资源
最近更新 更多