No RenderPartial from a different folder

3 June, 2009 (10:17) | ASP.NET MVC | By: twagner

Maybe this is not supposed to work out of the box in the manner I thought…..

In order to render something similar to a user control in ASP.NET MVC one creates a partial view – which ends up with the extension of ascx.  This partial view can be embedded into and rendered through other pages (views) . That’s normally done via the RenderPartial method. It all works very well until you need to render a partial view that sits in another folder.Then for whatever reason the system starts to complain bitterly. Yes I know … your supposed to put it into the Shared folder. But somehow that doesnt sit well with my sense of organization. Why have one folder that has a bunch of partials from all over the place.  ( I have not tried adding sub folders to /Shared. Maybe thats an idea as well)

Seems to me that I should be able to create a partial view that has a table of data and then embed this partial view in a variety of other views- wherever I might need this list. And the places where I might need this list could be scattered all over the Views structure.

I am not the only one who ran into issues like this.  Here is a post by a fellow who had similar issues with entire pages.

An internet search turned up the following idea which may possibly do the job:

protected void Application_Start()
{
	var engine = (WebFormViewEngine) ViewEngines.Engines[0];
	engine.MasterLocationFormats = new string[] { "~/Views/{1}/{0}.master", "~/Views/Shared/{0}.master" };
	engine.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.aspx", "~/Views/{1}/{0}.ascx", "~/Views/Shared/{0}.aspx", "~/Views/Shared/{0}.ascx" };
	engine.PartialViewLocationFormats = new string[] { "~/Views/{1}/Partial/{0}.aspx", "~/Views/{1}/Partial/{0}.ascx", "~/Views/Shared/Partial/{0}.aspx", "~/Views/Shared/Partial/{0}.ascx" };
}