Search code examples
asp.net.netasp.net-mvcpartial-viewsasp.net-mvc-layout

Can an ASP.NET MVC PartialView have a _Layout


Can an ASP.NET MVC PartialView directly use a Layout?

MSDN

I have looked at MSDN for PartialViewResult and compared with ViewResult:

ViewResult has a property MasterName that PartialViewResult does not.

But.. I can still define the Layout property in the partial views Razor.

Background

I am currently remediating a large code base with a huge amount of partial views used to fill iframes. Ideally they would be converted to normal views (ideally we would not use iframes) but I was wondering about just adding a layout to these partials to take out the <head> element at least so that we have more control over the script versioning (everything is replicated in each partial view). I'm looking for a light-touch solution since much of the code is expected to be thrown away.


Solution

  • Yes, even in Partial View also we can set Layout property which turns Partial View as normal View with Layout.

    @{
        Layout = "~/Views/Shared/_Layout.cshtml";
     }