Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does RenderPartial figure out where to find a view?

Ok. Googling fail probably and I remember reading about this a while back but can't find it.

I have a View and a Partial View in different directories. In a view I say @Html.RenderPartial("[partial view name]"); how does RenderPartial figure out where to look? It must be a convention but what is it?

My view is in: WebRoot\Views\Admin\ folder and partial is at WebRoot\Views\Admin\Partials

Not sure if this the right set up.

I'm using MVC 3 (Razor engine)

like image 538
dev.e.loper Avatar asked Jan 19 '23 14:01

dev.e.loper


2 Answers

you can, but you have to register the routes, to tell the view engine where to look for. example in Global.asax.cs you'll have:

ViewEngines.Engines.Add(new RDDBViewEngine()); 

and the class is:

public class RDDBViewEngine : RazorViewEngine
{
    private static string[] NewPartialViewFormats = new[] {         
        "~/Views/Shared/Partials/{0}.cshtml" ,       
        "~/Views/{0}.cshtml"
    };

    public RDDBViewEngine()
    {
        base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(NewPartialViewFormats).ToArray();
    }

}

{0} is for all the subfolders with partials.

like image 131
zdrsh Avatar answered Feb 04 '23 20:02

zdrsh


Locating views is the responsibility of the ViewEngine. The WebFormViewEngine was the one originally shipped with MVC 1, and you can see the paths it searches on codeplex. Note that it searches the same paths for views and partial views.

The CshtmlViewEngine (Razor) introduced with MVC 3 (or rather WebMatrix) searches similar locations but looks for different extensions.

like image 39
Morten Mertner Avatar answered Feb 04 '23 20:02

Morten Mertner