代码之家  ›  专栏  ›  技术社区  ›  Juan Salvador Portugal

EditorFor和ApplyFormatInEditMode

  •  0
  • Juan Salvador Portugal  · 技术社区  · 6 年前

    我正在使用MVC5,在格式化十进制数时遇到问题

    我的模型中有一个精度为18,5的十进制数,我想用它来编辑 EditorFor 但它忽略了 DataFormatString 尽管 ApplyFormatInEditMode true

    [DisplayFormat(DataFormatString = "{0:#.#####}", ApplyFormatInEditMode = true)]
    public decimal? Example{ get; set; }
    

    使用数字 4

    如果我将其呈现为:

    @Html.DisplayFor(x => x.Example) //result: 4 (this is what i want!)
    @Html.TextBoxFor(x => x.Example) //result: 4.00000
    @Html.EditorFor(x => x.Example) //result: 4.00000
    

    我该怎么处理?

    谢谢!

    1 回复  |  直到 5 年前
        1
  •  1
  •   user3559349 user3559349    6 年前

    结果 @Html.EditorFor(x => x.Example) 在使用内置默认模板时将是4,而不是4.00000,因此我只能假设您必须有一个自定义 EditorTemplate decimal? (检查 EditorTemplates 文件夹 /Views/Shared Views/YourControllerName 名为的文件的文件夹 Decimal.cshtml ).

    TextBoxFor() 是正确的。这个 [DisplatFormat] 只有在使用 DisplayFor() EditorFor() . 使用 文本框for() ,需要使用接受格式字符串的重载

    @Html.TextBoxFor(m => m.Example, "{0:#.#####}") // result: 4
    
    推荐文章