Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a question regarding ASP sections and partial views.

What ser the benefit of using one over another?

Because if I have a menu, i guest that it could be both as a section, but also as a partial view.

So what is the difference?

share|improve this question
add comment

2 Answers

A partial view is used when you have some view component (optionally with it's own model) that is used in more than one place, or is better separated in to a separate file (for readability, for example).

A section is placed in a master page, and allows individual views to populate those areas - they can mandatory or optional. There aren't really pro's and con's in a comparison between the two, since they serve different purposes.

For your menu example, you may have some mark-up that builds a menu (from a collection) which you would want to to use in many places, then a partial view would be the best option.

If you have a single menu on a page, whose contents change based on the page, then you might use a section to require that the view populates the menu (you may use a partial to populate that menu section too).

share|improve this answer
    
So a section is basically an empty container? Can a section be populated with a partial view? –  Robert Harvey Mar 26 '13 at 17:23
    
Yes, the section acts like a empty container in the master page which can(/should) be populated by the view by any means (including a partial). Whilst they are conceptually an empty container, there is most often some semantics and expected content associated with them (such as the menu). –  AndyBursh Mar 26 '13 at 17:26
add comment

A partial view works well with Model Binding. You can "type-bind" a partial view so that it will "bind" to the model provided.

@Html.Partial("_NavigationMenu", Model.Navigation)

A Section is basically like a "placeholder" or "literal" which will just output whatever code is placed within the region in it's appropriate location defined in the "parent".

@section JavaScript{
    //some code to be included on the "parent" view 
}

Generally, I use sections for various ad-hoc script requirements, or CSS, and partial views for DOM elements like formatting (you could use extensions too) / menus / navigation, etc... same idea as the old ".ascx" controls.

Also:

Partial views do not respect section declarations (by design) and therefor cannot be used.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.