8

In MVC 3 Beta, is there a difference between the templates MVC 3 Partial Page (Razor) and MVC 3 View Page with Layout (Razor) ?

I added a partial page (_partialList) to my application. Now when I return only the partial view, it applies the Layout present in _ViewStart.cshtml - acting very much like a stardard view page with layout.

    if (Request.IsAjaxRequest())
        return View("_partialList", someModelData);

How does a "partial" page distinguish itself from a standard view page with layout ? Will the two behave differently in any particular scenario?

5 Answers 5

10

If you don't want to apply the layout return a PartialView instead of View:

if (Request.IsAjaxRequest())
    return PartialView("_partialList", someModelData);
1
  • But I am still not sure why two templates exist when they basically do the same thing (i.e. the partial view / page with layout)?
    – Preets
    Commented Oct 13, 2010 at 17:49
9

Darin's response solves your practical issue of not wanting the layout to be applied.

Regarding the difference between the two, in Razor they are practically the same because both full pages and partials use the same extension and have the same base class.

The reason why there is different UI is because in the Web Forms view engine the two are implemented with different extensions and different base classes, which is why to seperate templates are necessary.

3
  • I understand the web forms part of it i.e. page & user control - two different concepts. But I'm afraid I don't quite get what implication the web form view engine has for razor.
    – Preets
    Commented Oct 14, 2010 at 11:42
  • @Preets The WebForms view engine has no implication for Razor at runtime. However, the Visual Studio tooling (the Add View dialog) supports both view engines and therefore requires that Razor comes with 2 options even though they are currently functionally equivalent.
    – marcind
    Commented Oct 14, 2010 at 17:59
  • @marcind - Thanks to your tutorial, I started getting the hang of Nested Layouts with Sections. I am still trying to understand Redefining Sections vs. Partial Pages. This response implies there is perhaps no difference. Based on my first question (stackoverflow.com/q/48027614/1735836) and my second question (stackoverflow.com/q/48082955/1735836), is there a best practices approach or can you offer some advice on which approach you might use to add content in a new tab in a tabular view without blowing away content in other tabs? Thank you for any help you can provide.
    – Patricia
    Commented Jan 3, 2018 at 18:29
3

Add the following code to your page, and the view engine will not apply the layout to it.

@{
    Layout = null;
}
2

Views have this @{ View.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; }

and partial views don't

0
0

I don't think there is any difference.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.