29

I'm running MVC3 with Razor and noticed that decimal values are truncated to 2 decimal places when in edit mode. I've managed to get round it by annotating my property with a display format. This doesn't seem like a very good solution as I'll have to remember to do this for every new view I generate (or update my templates).

I have checked the value returned by our service to the controller and it is correct at 1.144, but when bound to the view it comes out as 1.14 in the TextBox

ViewModel Property

[Required]
[Display(Name = "Unit Price")]
public decimal UnitPrice { get; set; }

.cshtml Code

@Html.LabelFor(model => model.UnitPrice) 
@Html.EditorFor(model => model.UnitPrice) 
@Html.ValidationMessageFor(model => model.UnitPrice)

If I decorate the property with the following then it works.

[DisplayFormat(
               ApplyFormatInEditMode = true, 
               DataFormatString = "{0:0.00###########################}", 
               NullDisplayText = "")]

Any Ideas?

Greg B
  • 14,597
  • 18
  • 87
  • 141
Craig
  • 293
  • 1
  • 3
  • 4
  • Since `decimal` is often used in financial related scenarios, possibly MVC is trying to be too smart for its own good here. – Matt Greer Mar 25 '11 at 04:54
  • It's not MVC doing this. Do you have a decimal template somewhere with it's own truncation? – Ryan Mar 25 '11 at 05:16

3 Answers3

33

That's how the default Decimal editor template is defined:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script runat="server">
    private object ModelValue {
        get {
            if (ViewData.TemplateInfo.FormattedModelValue == ViewData.ModelMetadata.Model) {
                return String.Format(
                    System.Globalization.CultureInfo.CurrentCulture,
                    "{0:0.00}", ViewData.ModelMetadata.Model
                );
            }
            return ViewData.TemplateInfo.FormattedModelValue;
        }
    }
</script>
<%= Html.TextBox("", ModelValue, new { @class = "text-box single-line" }) %>

Notice the {0:0.00} format.

So you have two possibilities:

  1. Use double instead of decimal as type in your model
  2. Modify the default editor template by creating a custom ~/Views/Shared/EditorTemplates/Decimal.cshtml which might simply look like this:

    @Html.TextBox(
        "", 
        ViewData.TemplateInfo.FormattedModelValue, 
        new { @class = "text-box single-line" }
    )
    

You probably might want to modify the display template as well.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
11

IMO, this article has a better option:

html-editorfor-with-3-decimal-places

I used this code to display up to 4 decimal digits in my EditFor:

    [Display(Name = "Discount Percentage")]
    [Range(0, 100.0)]
    [DisplayFormat(DataFormatString="{0:0.0000}", ApplyFormatInEditMode=true)]
    public Decimal? DiscountPercent { get; set; }
Community
  • 1
  • 1
TechSavvySam
  • 1,382
  • 16
  • 28
  • this is best solution presented – Jay Sep 28 '16 at 12:22
  • 3
    Nice, that works well and is better than creating a custom editortemplate in my opinion. If you want the decimal points to only show if the decimal has decimal points, replace the 0s with #s: `[DisplayFormat(DataFormatString = "{0:0.####}", ApplyFormatInEditMode = true)]` – germankiwi Mar 07 '17 at 04:15
9

If you don't need the functionality of the 'EditorFor' HtmlHelper, you can simply swap it out for the 'TextBoxFor' and it shouldn't truncate your decimal value...

JTech
  • 3,420
  • 7
  • 44
  • 51