Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Editor For Double showing scientific notation

I've got a property of my model that is of type double. One of my items has a value of 0.000028, but when my edit view is rendered, the editor for this value shows as 2.8e-005.

Aside from this being confusing to my users, it also fails my regular expression validation of

    [Display(Name = "Neck Dimension")]
    [RegularExpression(@"[0-9]*\.?[0-9]+", ErrorMessage = "Neck Dimension must be a Number")]
    [Range(0, 9999.99, ErrorMessage = "Value must be between 0 - 9,999.99")]
    [Required(ErrorMessage = "The Neck Dimension is required.")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:F20}")]
    public double? NeckDimension { get; set; }

How do I get this field to display? I've got this bit of code (shown below) that will render a decimal like I want, but I'm not sure where to implement it.

 var dbltest = 0.000028D;
 Console.WriteLine(String.Format("{0:F20}", dbltest).TrimEnd('0')); 

I use the property NeckDimension in two places, and edit view and a display view. Here is how each are being rendered.

@Html.TextBoxFor(model => model.NeckDimension, new { style = "width:75px;" })

@Html.DisplayFor(model => model.NeckHDimension)

UPDATE Apparently the DisplayFormat won't work with TextBoxFor. I attempted to change my @Html.TextBoxFor to an Html.EditorFor and giving it a class but it failed with the following exception.

The model item passed into the dictionary is of type 'System.Double', but this dictionary requires a model item of type 'System.String'

This old code still works:

@Html.TextBoxFor(model => model.NeckDimension, new { style = "width:75px;" })

This code gave the exception:

@Html.EditorFor(model => model.NeckDimension, new {@class = "formatteddecimal"})

It looks like my options are fix this with javascript or fix it with an editor template, but I don't have the time to research and learn the 2nd option at this point.

SOLUTION:

I created an editor template for double? as follows.

@model double?
@{
    var ti = ViewData.TemplateInfo;
    var displayValue = string.Empty;
    if (Model.HasValue) { 
        displayValue = String.Format("{0:F20}", @Model.Value).TrimEnd('0');
    }
    <input id="@ti.GetFullHtmlFieldId(string.Empty)" name="@ti.GetFullHtmlFieldName(string.Empty)" type="text" value="@displayValue" />
}
like image 836
Jeff Reddy Avatar asked Oct 03 '11 18:10

Jeff Reddy


1 Answers

You could decorate the property on your view model with the [DisplayFormat] attribute:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:F20}")]
public double Foo { get; set; }

and now in your strongly typed views simply:

@Html.DisplayFor(x => x.Foo)

or if it is for editing:

@Html.EditorFor(x => x.Foo)

Another possibility if you wanted to apply this format for all doubles in your application or per controller is to write a custom display/editor template.

like image 70
Darin Dimitrov Avatar answered Nov 15 '22 20:11

Darin Dimitrov