Html.DisplayFor decimal format?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 22:39:26

Decorate your view model property with the [DisplayFormat] attribute and specify the desired format:

[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):

@string.Format("{0:N}", Model)

and then:

@Html.DisplayFor(x => x.TAll, "MyDecimalTemplate")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!