2

Consider a project with 2 Areas.

/Areas/Blog /Areas/Dashboard

Now say that my Blog area has an editor for the type SpecialBlog. /Areas/Blog/Views/Blog/EditorTemplates/SpecialBlog.ascx

What if a view that is part of my Dashboard Area would like to display a special blog?

The following code works from Views inside the "Blog" Area but not from the "Dashboard" Area.

Html.EditorFor (model => model)  // model is type SpecialBlog

Even providing the path fails,

Html.EditorFor (model => model, "~/Areas/Blog/Views/Blog/EditorTemplates/SpecialBlog.ascx")

The one thing I can get working is

Html.RenderPartial (Model, "~/Areas/Blog/Views/Blog/EditorTemplates/SpecialBlog.ascx");

But any routing within the SpecialBlog then fails. (ie. it has its own Html.EditorFor calls to other editor templates in the Blog Area).

Am I doing something fundamentally wrong?

Justin
  • 10,667
  • 15
  • 58
  • 79

2 Answers2

0

I had the same problem, I wanted to use the below

~/Areas/ActivityPicker/Views/ActivityPicker/EditorTemplates/ActivityPicker.cshtml

This is far from gospel but I don't think you can do this (In MVC 5). When I tried and debugged through it it ended up at the below.

"EditorTemplates/~/Areas/ActivityPicker/Views/ActivityPicker/EditorTemplates/ActivityPicker.cshtml"

i.e. it added EditorTemplates to the beginning.

It then looked through the usual places to find this, i.e. the View and the immediate Area's Views + the Editor Templates, because it doesn't start with '~' it won't check all locations, only the common View and the current Area (check in VirtualPathProviderViewEnginer::IsSpecificPath)

Also replacing '~' with '../' doesnt help, i.e. you can't do something like the below:

"EditorTemplates/../../../Areas/ActivityPicker/Views/ActivityPicker/EditorTemplates/ActivityPicker.cshtml"

for the same reason

Even if this worked I wouldn't want to do it as then you'll be replying on a side effect which could break in a subsequent release

So, in summary I don't think you can at present, would be happy to be told otherwise as it seems like a limitation to me. I want multiple instances of the same View on a page, Editor Templates are the advised route for this but you can't use them between different areas

tony
  • 2,178
  • 2
  • 23
  • 40
0

I believe that putting this in the Shared folder will work.

If you don't want to put in the main /shared folder, you could try setting up another path in your ViewEngine (add at Application_start, or derive from the one you're using) to something like /areas/shared folder (this means, creating the folder) for all the areas and putting the templates there.

Linkgoron
  • 4,866
  • 2
  • 26
  • 26