Question

I'm having some issues with a view model in my project that contains an number of doubles in them

ie.

public class MyViewModel
{
    public double Left { get; set; }
    public double Right { get; set; }
    public double Top { get; set; }
    public double Bottom { get; set; }
}

In my view I have

@Html.EditorForModel

If there is no other custom templates in my project this renders correctly and I get a textbox with a double on in it.

As soon as I put a string template in View/Shared/EditorTemplate and put anything inside of it whatever is in that template gets rendered instead of the double template.

I would have thought that even though I have overriden the string template the order for selecting templates would remain the same, and the double template would be choosen as it was originally before the string template existed.

The string template consisted of only (no @model )

<h2>String</h2>

Am I missing something here, why is the string template choosen here?

I've determined I can put a double template in the views shared folder and it will render that instead but it seems like I shouldn't have to do that.

EDIT:

As per Darin's answer I created my own double editortemplate based on the decimal template.

@using System.Globalization
@functions {

    private object FormattedValue
    {
        get
        {
            if (ViewData.TemplateInfo.FormattedModelValue == ViewData.ModelMetadata.Model)
            {
                return String.Format(CultureInfo.CurrentCulture, "{0:0.00}", ViewData.ModelMetadata.Model);
            }
            return ViewData.TemplateInfo.FormattedModelValue;
        }
    }

}

@Html.TextBox("", FormattedValue, new {@class = "text-box single-line"})
Was it helpful?

Solution

Am I missing something here, why is the string template choosen here?

In both cases it's the String template being chosen. There's no default template for the double type. Here's how the default string template looks like:

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

I've determined I can put a double template in the views shared folder and it will render that instead but it seems like I shouldn't have to do that.

Well, if you are not satisfied with the default template for the double type (which is string.cshtml) you could always write a custom template for it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top