Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of Partial Views in Asp.net MVC

Tags:

Ive noticed that there seems to be no real difference between a view and a partial view. For instance, one can create a view but can render it as a partial view by using

@Html.Partial("ViewName") 

or by specifying that its action return it as

return PartialView(); 

Ive noticed that the opposite is also the case - ie, one can create a partial view but if it is returned as a full view, it will be displayed with the default layout for the views.

My question is this - When adding a new view in Visual Studio, one is given the option of creating a view that is partial or not. Isn't this redundant, since a view can be rendered as both a partial and a full view anyway?

like image 455
x1886x Avatar asked Sep 18 '13 12:09

x1886x


People also ask

What is the purpose of partial view in MVC?

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 advantage of partial view in MVC?

Advantages of Partial View in ASP.net MVC: using Partial View in ASP.NET MVC has following advantages: Enhances reusability by packaging up common website code instead of repeating the same in different pages. Easy to maintain. Changes in future are simple to accommodate.

What is difference between view and partial view in MVC?

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.

Where are partial views stored in MVC?

By default ASP.NET MVC stores all the views associated to a controller inside a sub-folder of Views folder. On the same lines partial views and layout pages are stored inside Shared sub-folder under Views folder.


2 Answers

There is difference between views and partial views, and the difference is more about their usage, rather than technical.

View is meant to be used as full page of your application, it needs layout, <html> and <title>. Partial views are more like reusable parts of other views. Partials do not represent full pages, they are inserted into other views.

From technical point of view, return View("SameView"); renders view including layout page, and returning that same view by return PartialView("SameView"); renders contents, but omits contents of layout page.

like image 112
archil Avatar answered Sep 23 '22 06:09

archil


No difference - it's true. But when you say "Partial View" all your teammates understand that you mean reusable views that will be used in many places across the website.

like image 30
Andrei Avatar answered Sep 19 '22 06:09

Andrei