Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I define the location of a custom editor template when using MVC areas?

It is my understanding that the location is:

~/Views/Shared/EditorTemplates/ViewModelName

However I have many Views folders using areas. Can I define the file to use with some parameter of the call to the

@Html.EditorFor( ...
like image 502
Samantha J T Star Avatar asked Feb 23 '23 08:02

Samantha J T Star


1 Answers

Those are default lookup paths which RazorViewEngine will search for editor template, in this order:

"~/Areas/{area}/Views/{controller}/EditorTemplates/{templateName}.cshtml",
"~/Areas/{area}/Views/Shared/EditorTemplates/{templateName}.cshtml",
"~/Views/{controller}/EditorTemplates/{templateName}.cshtml",
"~/Views/Shared/EditorTemplates/{templateName}.cshtml",

If not specified, templateName value defaults to object type (in your case 'ViewModelName'). If template with this name is not found by MVC will fall back to resolve rendering using known build-in templates (int, string, collection, object, etc).

You can specify template name to override defaults:

@Html.EditorFor(m => m.MyDate, "_MyTemplate")

You can also specify relative paths:

@Html.EditorFor(m => m.MyDate, "../_MyTemplate")

You cannot specify full paths in any form (ex:"~/Views/Custom/EditorTemplates/ViewModelName") and you should never specify extension in template name (ex: '_MyTemplate.cshtml', or '_MyTemplate.vbhtml')!

like image 84
Nenad Avatar answered May 09 '23 04:05

Nenad