2

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.

Scotty.NET
  • 12,533
  • 4
  • 42
  • 51
  • If you're rendering these in an iframe instead of as child actions, shouldn't they be invoked as Views anyway, instead of PartialViews? – StriplingWarrior Jul 01 '14 at 16:34
  • @StriplingWarrior - you are right, but that would be a more invasive change. Our goal, for now, is to get a handle on the many versions of jQuery (jQueryUI, etc) used throughout the app. – Scotty.NET Jul 02 '14 at 07:06

2 Answers2

4

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";
 }
malkam
  • 2,337
  • 1
  • 14
  • 17
0

Yes.

You can use a layout within a layout:

Layout View Layout PartialView

We have an example of this in our codebase.

Lemiarty
  • 124
  • 10