45

Can you point MVC to a folder other than the default ones (Views/Shared/EditorTemplates & Views/Shared/DisplayTemplates)? I'd like to either put them in subfolders below those, or in other folders outside the Shared folder.

For example, if I have an editor template under this folder:

~\Views\Order\ProductModel.cshtml

How can I tell my EditorFor template to use this tempalte name?

I tried fully qualifying it, but this doesn't work:

@Html.EditorFor(m => m.ProductModel, @"~\Views\Order\ProductModel.cshtml")

I've tried using forward slashes & backslashes, with/without .chstml, every combination I can think of. I'm beginning to think this isn't supported, but I can't imagine why it wouldn't be.

Jerad Rose
  • 15,235
  • 18
  • 82
  • 153

3 Answers3

55

No, I am afraid you can't do this.

For example, if I have an editor template under this folder

That's no longer an editor template. It's a partial. If you want to share editor templates between different controllers you can simply put them inside the ~/Views/Shared/EditorTemplates folder.

As far as subfolders are concerned then you could do this:

@Html.EditorFor(x => x.Foo, "Order/ProductModel")

which will render the ~/Views/CurrentController/EditorTemplates/Order/ProductModel.cshtml or ~/Views/Shared/EditorTemplates/Order/ProductModel.cshtml editor template.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 7
    This makes sense. I didn't know you could have Editor/DisplayTemplate folders beneath the controller's view folder. That accomplishes what I'm trying to do. Thanks Darin. – Jerad Rose Oct 20 '11 at 20:33
  • The second path: `~/Views/Shared/EditorTemplates/Order/ProductModel.cshtml` does not gets found for me... is there a gotcha? – Dmitry Efimenko Jul 31 '12 at 19:27
  • Actually it finds it, but gives me an error: `The model item passed into the dictionary is of type 'System.Collections.Generic.List``1[Whatever.Models.ProductModel]', but this dictionary requires a model item of type 'Whatever.Models.ProductModel'.` – Dmitry Efimenko Jul 31 '12 at 19:32
  • in your example Foo is a `List` – Dmitry Efimenko Jul 31 '12 at 19:33
  • 1
    @Dmitry See here - http://stackoverflow.com/questions/5818351/problem-with-mvc-editorfor-named-template. tldr - By explicitly setting your EditorFor to use a particular editor template, you are telling it to use that editor template for the whole enumeration, instead of allowing the enumerable to be templated by default, in turn using the template for each enumerated item" – Bookamp Aug 03 '12 at 16:52
  • @Bookamp I understand the general purpose of `EditorTemplates` and the way they work, which is what you seem to be trying to explain to me. The only trouble I had is having one in a subfolder of `EditorTemplates` folder. For some reason when I specify to use that template, it starts requiring a `List` instead of just `MyModel` – Dmitry Efimenko Aug 03 '12 at 17:04
  • Hi @damin-dimitrov, If my view file outside of application, how can i proceed? I could not read the file. – sam anderson Aug 24 '16 at 06:34
5

Old question, but... proper way to add display/editor template for specific controller is to add it in DisplayTemplates or EditorTemplates subfolder.

Assuming that, in your example, you have OrderController, you can simply put display template for your model into sub-folder, like this:

~\Views\Order\DisplayTemplates\ProductModel.cshtml

Then, call display template normally from your view (ex: from `~\Views\Order\Index.cshtml):

@Html.DisplayFor(m => m.MyProductModelProperty)
Nenad
  • 24,809
  • 11
  • 75
  • 93
  • 1
    This answer better addresses the issue of having Editor/Display templates when they don't make sense to be shared but rather to be contained within a certain controller. This avoids trying to reuse what cannot sometimes be reused for the sake of maintainability. One vote for this answer. – Reuel Ribeiro Oct 19 '17 at 17:29
3

If you do this:

@Html.EditorFor(x => x.Foo, "Order/ProductModel")

it won't parse Foo as a collection and apply your editor template to each item. It will rather assume that your editor template should be applied to the collection as a whole.

If you want to apply editor template to each item individually, just place it in EditorTemplates folder under your view folder (as it will have precedence) and use the default syntax:

@Html.EditorFor(x => x.Foo)

Of course, the name of the editor template has to match the type of items in your collection.

lukeguy
  • 118
  • 1
  • 8