Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

asp.net mvc - 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:

~ViewsOrderProductModel.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, @"~ViewsOrderProductModel.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...