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.
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.
The primary difference between the two methods is that Partial generates the HTML from the View and returns it to the View to be incorporated into the page. RenderPartial, on the other hand, doesn't return anything and, instead, adds its HTML directly to the Response object's output.
A view is an HTML template with embedded Razor markup. Razor markup is code that interacts with HTML markup to produce a webpage that's sent to the client. In ASP.NET Core MVC, views are .cshtml files that use the C# programming language in Razor markup.
A Strongly Typed view means it has a ViewModel associated to it that the controller is passing to it and all the elements in that View can use those ViewModel properties. You can have strongly typed partials as well. Meaning that piece of Html needs specific data so you type it to a certain ViewModel.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With