我使用的是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我该如何使用它呢?
谢谢!
发布于 2018-08-17 07:22:55
当使用内置的默认模板时,@Html.EditorFor(x => x.Example)的结果将是4,而不是4.00000,因此我只能假设您必须为decimal?的类型定义一个自定义EditorTemplate (检查/Views/Shared和Views/YourControllerName文件夹中的Decimal.cshtml文件)。
使用TextBoxFor()时看到的结果是正确的。只有在使用DisplayFor()或EditorFor()时,才会考虑[DisplatFormat]属性。要使用TextBoxFor()格式值,需要使用接受格式字符串的重载
@Html.TextBoxFor(m => m.Example, "{0:#.#####}") // result: 4https://stackoverflow.com/questions/51881439
复制相似问题