DisplayFormat does not apply to decimal value

I have a model property that I'm trying to execute using the EditorFor template, and I'm trying to apply formatting using the DisplayFormat attribute. However, it does not work at all - it is completely ignored.

Here is my template:

@model System.Decimal? @Html.TextBoxFor(m => m) 

Here is my model:

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

Here is my view:

 @Html.EditorFor(m => m.Retail) 

But it creates a text box with the following value:

189.9900

It seems pretty straight forward, but it doesn't work, and I have no idea why.

UPDATE: Just for beats, I tried it with the DisplayFor template and it worked:

 @Html.DisplayFor(m => m.Retail) 

So why did the DisplayFor pattern work, but not the EditorFor pattern, when did I set ApplyFormatInEditMode to true?

UPDATE 2: It doesn't matter, the reason is that my decimal display pattern was hard-coded that way. Therefore, my display template also does not work.

+8
decimal asp.net-mvc asp.net-mvc-3 mvc-editor-templates
source share
3 answers

Darin Dimitrov posted this answer , and I was able to get it to work using his solution:

 @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue) 

A bit rude, IMO, that this does not work w / TextBoxFor , but at least it works.

+3
source share

DisplayFormat will not work like this; if you manually create a text box for a property, it will not come into play. This will only work if you have done

 @model System.Decimal? @Html.DisplayFor(m => m) 
+2
source share

Try in this format, it displays 18.999.00

 [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:N}")] 
+2
source share

All Articles