LetThereBeByte
LetThereBeByte

Reputation: 75

ASP.NET MVC Editor Templates

I have a question about Editor Templates. For example I have a model with date field, product prize field and weight of product field. I have successfully created Editor Templates for DateTime and Decimal. I'm given to understand that when I use EditorFor for any field it takes template depending on a data type of the field.

So when I use: EditorFor(m=>m.DateOfBirth) which is DateTime format it looks for DateTime template in EditorTemplates folder and EditorFor(m=>m.ProductPrice) which is double it looks for Double template, etc.)

Question is how can I differ that I want to use one Decimal template with string format ##0.00 for (double)ProductPrize and another template with format ##0.0000 for (double)ProductWeight?

Any help would be appreciated

Upvotes: 2

Views: 3056

Answers (2)

Marc
Marc

Reputation: 6771

Define it in your model using the UIHint attribute:

[UIHint("MySpecificEditorTemplate")]
public ProductPrice {get;set;}

And EditorFor will use the MySpecificEditorTemplate.cshtml for example.

Upvotes: 5

Sergii Kudriavtsev
Sergii Kudriavtsev

Reputation: 10487

You may want to use an overload of EditorFor, which accepts template name as second parameter, like:

EditorFor(m=>m.ProductPrice, "PriceEditorTemplate")

More info: MSDN

Upvotes: 8

Related Questions