Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 424
Jerad Rose Avatar asked Oct 20 '11 20:10

Jerad Rose


2 Answers

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.

like image 91
Darin Dimitrov Avatar answered Sep 23 '22 18:09

Darin Dimitrov


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) 
like image 44
Nenad Avatar answered Sep 24 '22 18:09

Nenad