Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View() vs. PartialView()

Tags:

asp.net-mvc

The View() method can load Partial Views.

Is the difference between View() and PartialView() is that View() can load views and partial views and PartialView() can only load partial views?

like image 827
burnt1ce Avatar asked Nov 17 '10 23:11

burnt1ce


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 the difference between view and partial view in MVC?

View can basically contains a complete markup which may contain a master view(or master page) with all the design(s) etc. whereas Partial view is only a portion of page or a small markup which don't have master page. It is basically used as user control in mvc and it can be used at more than one views.

What is a partial view?

A partial view is a Razor markup file ( . cshtml ) without an @page directive that renders HTML output within another markup file's rendered output. The term partial view is used when developing either an MVC app, where markup files are called views, or a Razor Pages app, where markup files are called pages.

What is the difference between partial and RenderPartial in MVC?

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.


2 Answers

It's up to a view engine to decide if they want to treat partial views different from regular views.

For example, in the WebFormViewEngine there is no difference.

In the new ASP.NET MVC 3 RazorViewEngine there are some differences. Only regular views will have the "_viewstart.cshtml" pages run because they are meant for things such as setting up layout pages.

like image 154
Eilon Avatar answered Sep 19 '22 08:09

Eilon


I think the biggest difference is about the use of the _Layout page:

  • PartialView(): the razor engine will get the view (e.g. index.cshtml) without any layout page (_layout.cshtml).
  • View(): the engine will get your view (e.g. index.cshtml) and then appends the content of this view inside the layout page (_layout.cshtml)
like image 22
fabriciorissetto Avatar answered Sep 22 '22 08:09

fabriciorissetto