What is the difference between a View and a PartialView in ASP.NET MVC?

37,097

Solution 1

In theory, the answer is: A partial view is a "sub-view" that you embed within a main view - something that you might reuse across multiple views, like a sidebar.

In practice, the answer is: Very little.

In theory, partial views are more lightweight than standard views, but it's perfectly OK to pass a "regular" view to RenderPartial and the performance seems to be exactly the same. I frequently use regular .aspx views as "partial" views because you can make them reference a master view in order to provide templated content like what you can do with UserControls in ASP.NET WebForms. See here.

Partial views are more like web parts on a portal - they are completely self-contained objects. Use them if the layout is simple and static, or if you're annoyed by the Intellisense errors when you don't have the <html> and <body> tags in a standard View.

Solution 2

It works like that:

  • return View() the view content goes in the @RenderBody() of the /Shared/_Layout.cshtml

  • return PartialView() it returns only the view content

Solution 3

Views are the general result of a page that results in a display. It's the highest level container except the masterpage. While a partial view is for a small piece of content that may be reused on different pages, or multiple times in a page.

If you're coming from webforms, view is similar to a web content form, while a partial view is like a user control.

Solution 4

If you come from a webforms background, think of PartialView as a usercontrol.

Solution 5

Look at StackOverflow.com site: Main site (View) contains components like:

  • Tags
  • Related
  • Ad

So Tags, related, Ad etc. can be composed as PartialViews. The advantage of this is that PartialViews can be simply cached by OutputCache instead of recreating all site: performance gain.

Share:
37,097
Ben Aston
Author by

Ben Aston

Updated on October 22, 2021

Comments

  • Ben Aston
    Ben Aston over 2 years

    What is the difference between a View and a PartialView in ASP.NET MVC?

    At first glance the need for both seems non-obvious to me.

  • Ben Aston
    Ben Aston over 14 years
    But, you could presumably envisage a system that simply enables "Views" to contain "Views", negating the need for an additional type?
  • Leandro
    Leandro over 14 years
    View that contains other Views is very handy. It's depend of you and it's you choice to have one big View or one View as set of smaller Views. All partial views should be independent of others and can have other types as return values.
  • Omu
    Omu about 14 years
    how about return View() and return PartialView(), it seems to me that there is no difference at all