How to add and use a razor editor template

2019-04-07 17:41发布

问题:

I have tried to find documentation or samples on how to incorporate razor editor templates into a project. the following answer on another thread may fix a problem I am having but I do not know how to incorporate it into my project. It is no doubt easy.

here is the solution I do not know how to incorporate: Client-side validation of input type date does not work

回答1:

Here is short info about adding EditorTemplate. In short words - you have to create directory EditorTemplates in Views/Shared and you should create a view with the same name as the model you want to create the editor for.

Remember that EditorTemplate for a model is used when calling Html.EditorFor with object of this model as argument. You should use them to provide editing possibilities, of course - if you want to create something that should only display data, use the same way to create DisplayTemplate (create DisplayTemplates directory). DisplayTemplate is rendered on Html.DisplayFor call with object of this model as argument.



回答2:

There's some useful background on the default templates used by Html.Editor(For) and Html.Display(For) and how to override them using views in the Views/Shared/EditorTemplates and View/Shared/DisplayTemplates directory of your project here.

I have a project on GitHub that contains Razor replacements for all of the default display and editor templates. Have a look at the demo web application - it should help you to see how things fit together. Then, copy the templates into your project and modify the parts you need.