Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.DisplayFor decimal format?

I have a decimal value for example: 59625879,00

I want to show this value like this: 59.625,879 or 59625,879

How can I do this with @Html.DisplayFor(x => x.TAll, String.Format()) ?

Thanks.

like image 622
AliRıza Adıyahşi Avatar asked Aug 22 '12 06:08

AliRıza Adıyahşi


1 Answers

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")
like image 137
Darin Dimitrov Avatar answered Nov 02 '22 05:11

Darin Dimitrov