Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Partial View and Layout?

I had used both the Partial View and also the Layout Concept in my Project i cannot able to differentiate. But what i am feeling is both doing the same work. Can anyone tell the brief idea about the Partial View and Layout and difference with example?

like image 253
DINESH C Avatar asked Jul 14 '14 06:07

DINESH C


People also ask

What is difference between partial view and 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.

What is layout view and 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.

Can partial view have layout?

Yes, even in Partial View also we can set Layout property which turns Partial View as normal View with Layout .

What is the purpose of a partial view?

Introduction to MVC Partial View Partial can be reusable in multiple views. It helps us to reduce code duplication. In other word a partial view enables us to render a view within the parent view.


2 Answers

In addition to Josh's answer, my aweeeesomeee paint skills would like to draw you a picture that should explain all..

Diagram displaying Layout, View and Partial View

Admit it... you're in awe...

You see the header and footer... you could even have partial view's there too.


EDIT...


Layout

To give you a different example of why you use each component (layout / view / partial view), imagine that you own a website that has 100 pages in total, and lets say you want to update the design of your website, how are you going to do it?

Updating each page individually would drive me insane, because your replicating your code constantly for every single page, just to update your design.

This is what the Layout view helps you solve, you use the Layout view to create a template for all of your pages.


View

Using our existing scenario of 100 page website, each page is going to have content that is unique, the View allows us to display this content whilst using our template from the Layout.


Partial View

Now lets imagine that we allow our visitors to comment on our pages, each comment must look consistent, and behave exactly the same as all the other comments throughout our website... To achieve this, you would use a Partial View which would act as a template for the comments that you receive on your website.

The benefits of doing this is that you don't have to repeat your code everywhere, you only have to create one Partial View to render any comment.

Diagram displaying Layout, View and Partial View

like image 54
Aydin Avatar answered Oct 03 '22 22:10

Aydin


Layouts allow you to generate a consistent look across your entire site. Think of them like Master pages of ASP.net.

What are Layouts?

You typically want to maintain a consistent look and feel across all of the pages within your web-site/application. ASP.NET 2.0 introduced the concept of “master pages” which helps enable this when using .aspx based pages or templates. Razor also supports this concept with a feature called “layouts” – which allow you to define a common site template, and then inherit its look and feel across all the views/pages on your site. - http://weblogs.asp.net/scottgu/asp-net-mvc-3-layouts

Partial views allow you to construct a view and render it inside of a parent view. For instance, say have a site that allows you to comment on an article. The section in which displays and allows a user to add a comment is a piece of reusable code that is inserted into all of the pages you wish the functionality to exist. What makes this important is that you can then modify that single partial view file to update every view that renders that partial instead of tracking down each page that implements that code individually.

Here is a Youtube Vid that helped me understand partial views rather well. https://www.youtube.com/watch?v=SABg7RyjX-4

edit: Additionally, the guy who created the linked vid has an entire library of playlists that may help a new MVC coders. He walks through a great deal of the MVC features with decent examples. https://www.youtube.com/user/kudvenkat

like image 21
Josh Avatar answered Oct 03 '22 22:10

Josh