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.

2
  • If you're rendering these in an iframe instead of as child actions, shouldn't they be invoked as Views anyway, instead of PartialViews? Commented Jul 1, 2014 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. Commented Jul 2, 2014 at 7:06

2 Answers 2

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";
 }
Sign up to request clarification or add additional context in comments.

Comments

0

Yes.

You can use a layout within a layout:

Layout View Layout PartialView

We have an example of this in our codebase.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.