asp.net-mvc Html.DisplayFor十进制格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12067375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Html.DisplayFor decimal format?
提问by AliR?za Ad?yah?i
I have a decimal value for example: 59625879,00
例如,我有一个十进制值: 59625879,00
I want to show this value like this: 59.625,879or 59625,879
我想像这样显示这个值:59.625,879或59625,879
How can I do this with @Html.DisplayFor(x => x.TAll, String.Format())?
我怎样才能做到这一点@Html.DisplayFor(x => x.TAll, String.Format())?
Thanks.
谢谢。
回答by Darin Dimitrov
Decorate your view model property with the [DisplayFormat]attribute and specify the desired format:
使用[DisplayFormat]属性装饰您的视图模型属性并指定所需的格式:
[DisplayFormat(DataFormatString = "{0:N}", ApplyFormatInEditMode = true)]
public decimal TAll { get; set; }
and then in your view:
然后在您看来:
@Html.DisplayFor(x => x.TAll)
Another possibility if you don't want to do this on the view model you could also do it inside the view:
另一种可能性,如果您不想在视图模型上执行此操作,您也可以在视图内执行此操作:
@Model.tAll.ToString("N")
but I would recommend you the first approach.
但我会推荐你第一种方法。
Yet another possibility is to write a custom display template for the decimal type (~/Views/Shared/DisplayTemplates/MyDecimalTemplate.cshtml):
另一种可能性是为十进制类型 ( ~/Views/Shared/DisplayTemplates/MyDecimalTemplate.cshtml)编写自定义显示模板:
@string.Format("{0:N}", Model)
and then:
进而:
@Html.DisplayFor(x => x.TAll, "MyDecimalTemplate")

