MVC template from folder other than default (EditorTemplates/DisplayTemplates)?
Asked Answered
D

3

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.

Debarath answered 20/10, 2011 at 20:22 Comment(0)
H
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.

Heisser answered 20/10, 2011 at 20:26 Comment(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.Debarath
The second path: ~/Views/Shared/EditorTemplates/Order/ProductModel.cshtml does not gets found for me... is there a gotcha?Positive
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'.Positive
in your example Foo is a List<ProductModel>Positive
@Dmitry See here - #5818851. 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"Pyatt
@Pyatt 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<MyModel> instead of just MyModelPositive
Hi @damin-dimitrov, If my view file outside of application, how can i proceed? I could not read the file.Bonanno
R
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)
Reflexive answered 18/5, 2015 at 8:59 Comment(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.Dg
W
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.

Warner answered 5/8, 2012 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.