简体   繁体   中英

ASP.NET MVC Razor - What is the difference between Html.Partial and RenderSection?

Both seem to allow you to take a section of a master layout and place it in another child file. I've been using RenderSection and then defining the section in the child files but I'm starting to wonder if this is not "best practice".

What is the difference between Html.Partial and RenderSection in a Razor MVC document?

Html.Partial renders a partial view. This is another razor view that you want to embed within the main view.

RenderSection is done in a layout template to mark a "stub" where certain code would go.

Typically you use Html.Partial to render reusable pieces of code into multiple pages-- such as a complex user-control.

You would normally use RenderSection in your layout to have the layout decide WHERE certain content goes in the output, and the section in the view that is being rendered determines WHAT that content is. A common use is to allow your views define scripts that that layout then places at the bottom of the html body after footers and things defined in the layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM