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

Viewed 39241

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.

7 Answers

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.

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.

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

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.

Consider a partialview like a control in webforms, the idea is the partial is reusable

View in Core Mvc

  • View Contains The layout Page(_ViewStart) Or Layout Property.

@{ Layout="_Layout"; }

  • Before Any View Rendered _ViewStart is Rendered.
  • View might have markup tags As
<!DOCTYPE html>   
 <html>
     <head>
     </head>
     <body>    
     </body> 
  </html>
  • View isn't Lightwight as Partial View.
  • View will Render in Layout by used @RenderBody() Method (Views/Shared/_Layout.cshtml).
  • Calling by use return View() inside Action.

Partial View

  • Partial view doesn't Contain Layout Page Layout Property
  • It does not verify for a viewstart.cshtml. We cannot put common code for a partial view within the viewStart.cshtml.page.
  • In MVC Partial view is designed specially to render within the view
  • We can pass a regular view to the RenderPartial method.
@{await Html.PartialAsync("Partial"); }

Example To Demo Partial View Is Strongly Type

in View

@model List<Student>;
@{
    Layout = "_Layout";
}  
 
<div>@await Html.PartialAsync("Partial",Model)</div>

in Partial View

@model List<Student>; <ul>
     @foreach (Student student in Model)
     {
        <li>@student.ID</li>
        <li>@student.Name</li>
        <li>@student.Age</li>
     }
 
     </ul>
  • SP.NET Core MVC looks for Partial View in the same way like Regular Views (i.e. in Views/ and Views/Shared folders). This means you can create controller specific partial views or shared partial views.

    Good Luck

Related