views:

46

answers:

4

I ve gone through sample asp.net mvc nerd dinner application... But still cant understand the point where and when should i go for partial views?

  • Is it similar to usercontrols in webformw?

Note: It would be helpful to see a partial view in action... Any sample...

+2  A: 

You should use partial views in two primary cases:

  1. When you need to reuse a similar "group of components" in multiple locations in a website (e.g. a "login form" might be used in different places in the website).

  2. When you have a significant amount of rendering logic for producing a particular section of a page and want to isolate it so that the page is cleaner/easy to edit normally in the same way you would put code functionality into its own method or class. A good example could be a navigation bar where you want to have the logic for rendering the navigation bar in one place, even though only your Site.Master might call it.

Graphain
+2  A: 

Anything you would use more than once. For instance on this page for SO, you see the listing of related posts over to the right. That is on multiple page, so why create it multiple times. You can pass data to the partial view to customize it based on certain criteria.

I do not like it for certain things like login, where I would rather take the user to a login page. However that scenario is used often.

Dustin Laine
+1  A: 

Partials should be used when you need to display the similar information on multiple views.

A simple sample would be a partial that displays a list of orders. On an account summary page you would use the partial to show recent orders. On an order list page you could use the same partial to display all orders ever placed.

ahsteele
A: 

Keep in mind that Mvc 2 partials do not maintain binding state across partials like EditTemplates do. This can be useful in some situations and frustrating in others. If you need binding state to be constructed as it is in templates, check out MvcContribs view types.

S. Hebert