ASP.Net MVC 3 Display-templates and Editor-Template Custom Location, how to?

I think you've had no answers, because there isn't one :-(.

Up vote 3 down vote favorite 1 share g+ share fb share tw.

I am going Nuts, I am using MVCContrib, to create pluggable site using Portable Areas, and everything is working well so far, except that when I started using MVC Templates, what is happening is if I put the The templates in the respective folder of the View it works, examples HostApplication/Views/Home/DisplayTemplates/FirstName. Cshtml HostApplication/Areas/PortableArea_Blog/Views/Home/DisplayTemplates/Auther. Cshtml but what I want really is the ability to create common templates Set and utilize it from either Host Application or Portable Area, so to do that I created a new Portable Area Called DisplayTemplates(to utilize MVCContrib Ability to compile Views), here is the portable Area structure DisplayTemplates |-Views |-CommentTemplate.

Cshtml now in my host Application I have created a Test Model and added UIHint Attribute public class HostModel { UIHint("~/Areas/DisplayTemplates/Comment. Cshtml") public string Name { get; set; } } but it is not working, so I thought it has something to do with Partial Views Location so I created a CustomView Engine to find Partial Views in that Location and registerd it in Global. Asax, here is a short idea about so I won't bore you with full code public class AreaViewEngine : RazorViewEngine { public AreaViewEngine() { // {0} = View name // {1} = Controller name // View locations ViewLocationFormats = new { "~/Areas/DisplayTemplates/{0}.

Cshtml" }; PartialViewLocationFormats = ViewLocationFormats; AreaPartialViewLocationFormats = ViewLocationFormats; } protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath) { return new RazorView(controllerContext, partialPath, null, true, new { "cshtml" }); } protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath) { return new RazorView(controllerContext, viewPath, masterPath, true, new { "cshtml" }); } } what is even more weird, is that it seems that that UIHint with Explicit location to Display Template, does not work, here is an example public class HostModel { //this works UIHint("FirstName") //this does not work UIHint("~/Views/Home/DisplayTemplates/FirstName. Cshtml") public string Name { get; set; } } and yes FirstName. Cshtml is in HostApplication/Views/Home/DisplayTemplates/FirstName.

Cshtml again sorry for the long post, but I gave up on finding a solution, so any help would be totally appreciated. Asp. Net-mvc asp.

Net-mvc-3 razor link|improve this question asked Mar 3 '11 at 2:32DevMania839418 66% accept rate.

2 amazingly 654 views yet no answer :( – DevMania Apr 5 '11 at 23:11.

I think you've had no answers, because there isn't one :-( I've searched high and low the last few days trying to find a solution to this (since as you mentioned, it's had a lot of views). Unfortunately I can't find any way to override this. I think you're stuck :( Edit: Came across this post for reading views from a database instead of the disk: ASP.

NET MVC load Razor view from database I wonder whether the DisplayTemplates are read this way. If so, you could try hijacking them and reading them from another location (instead of DB)?

I very much appreciate your reply man, I thought I am the only one on this, I will definitely read the link you sent and make test and will let you updated. Thanks again – DevMania May 4 '11 at 8:03.

Danny is correct. The Templates are found the same way that Partial Views are found. By default the WebFormViewEngine and RazorViewEngine are going to search the following locations for a template.

For display templates: ~/Views/{controller}/DisplayTemplates ~/Views/Shared/DisplayTemplates For editor templates: ~/Views/{controller}/EditorTemplates ~/Views/Shared/EditorTemplates I think the name of the sub-directories (i.e. , "DisplayTemplates" and "EditorTemplates") are hard-coded into MVC somewhere (I know it's open source and I could find it, but I'm not going to). I think the easiest way to change the location somewhat is to override the ViewEngine.

My custom ViewEngine is pretty complicated at this point, but I suspect you could get away with the following. Let's say you want your templates to be in ~/Views/Templates. Create a class that inherits from the view engine you're using now (probably WebFormViewEngine or RazorViewEngine).

Add an empty constructor. It should looks like this: namespace MySite { public class MySiteViewEngine : RazorViewEngine // (); A couple of notes about the above: The entry above will make it so that your view engine looks for the String display template at ~/Views/Templates/DisplayTemplates/String.cshtml. The location format in these view engines includes the file extension, so if you're using Razor/C# use "cshtml", Razor/VB use "vbhtml", WebForms add "aspx" and "ascx".

The way I'm doing it above, I'm adding my location format to the top of the list but keeping all the default locations. You might consider removing those. Watch the current formats and you'll see that you will also get a controller in the {1} position in the format, so if you wanted to have a Templates directory underneath every controller you could.

Careful, once you get started moving things around with a view engine, it gets addictive. You might find yourself moving everything around. Good luck.

Happened to run across the hard-coded values I mentioned in this post. They are in the MVC code in \mvc3\src\SystemWebMvc\Mvc\Html\TemplateHelpers.cs. There might be a way to override the TemplateHelpers class and rewrite the modeViewPaths dictionary, but it is probably more work than it is worth.

Just rewriting the dictionary still leaves the templates as a sub-directory of the PartialViews location without a override of ExecuteTemplate. – OneCleverMonkey Aug 15 '11 at 5:11.

Instead of creating a new ViewEngine you can easily modify the existing ones at runtime: private void FixMvcTemplateAreaBug(string areaName) { foreach (BuildManagerViewEngine viewEngine in ViewEngines. Engines) { List viewLocations = new List(viewEngine. PartialViewLocationFormats); foreach (var extension in viewEngine.

FileExtensions) viewLocations. Add("~/Areas/" + areaName + "/Views/Shared/{0}. " + extension); viewEngine.

PartialViewLocationFormats = viewLocations.ToArray(); } } Place the above in an appropriate location (like area registration) and you'll be fine.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions