Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 239
Ben Aston Avatar asked Jan 11 '10 17:01

Ben Aston


People also ask

What is the difference between View and Partialview?

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.

What is difference between partial and render partial view?

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.

What is a view in MVC?

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.

What is partial view and strongly typed view?

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.


2 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.

like image 87
Aaronaught Avatar answered Oct 20 '22 01:10

Aaronaught


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

like image 31
ThiagoVBDiniz Avatar answered Oct 20 '22 00:10

ThiagoVBDiniz