2

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

2 Answers 2

8

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

Sign up to request clarification or add additional context in comments.

2 Comments

Beat me to it, I'll remove my answer.
Thanks a lot for such a quick answer. It works like a charm. I think I will use this one since It's easier to edit without need to compile the project again once you've published the site.
5

Define it in your model using the UIHint attribute:

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

And EditorFor will use the MySpecificEditorTemplate.cshtml for example.

1 Comment

Thanks a lot for such a quick answer. This works also but I will use Sergeys solution since this is compiled in dll library which makes it harder to edit once the site is up and running

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.