19

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?

4 Answers 4

49

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

Sign up to request clarification or add additional context in comments.

7 Comments

Hello boss Aydin Adn, If suppose I use the Partial View it gives same output like you posted here. And also if suppose I use the Layout that also gives the same output like you posted here. Then why I have to use two different concepts to do a same work?
@DINESHC A layout is used once, when the page is generated. A partial can be used multiple times on a single page to repeat content and send data with it, as he graphically represented with the ARTICLEs. When you want to update the general format of the site, you edit the layout so the changes propagate throughout all of the pages that use a layout. If you want to change how the ARTICLE is displayed, you edit the partial view. The key is, you edit ONE file and the changes will propagate throughout all of the uses of your partial.
Absolutely, in fact it's highly recommended. Looking at the example in this answer, you use a layout to create the general structure, formatting and look of the site. You then use views to generate the html content that would generally be contained within the <body></body> tag. Within a view, you would use a partial to repeat smaller views. You should really read the material found: stackoverflow.com/a/24731124/507025. Additionally, you can always search Google for "asp.net mvc partial" and "asp.net mvc layout".
Thank you @Aydin Adn. Now I got the difference. The highlight is Within the page Partial view can used more than once, but Layout can be used only once.
Thank you @Josh. Now I got the difference. The highlight is Within the page Partial view can used more than once, but Layout can be used only once.
|
4

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

Comments

3

Non-technical explanation:

Layout is a foundation of the house, View is a single room in that house and PartialViews are windows in that room or sockets with electricity in walls.

1 Comment

Sir, already I known this. I want a answer with more clear example.
2

To make it simple Here is my answer:

1)

A layout is something that we can include once in a single page and we can use the same layout to any number of pages.

2)

A partial view is something that we can include the same content any number of times in a single page(where it is required) and can be used in any number of pages.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.