The roles and differences of @RenderBody, @RenderSection, @RenderPage, Html.renderpartial, html.renderaction

Source: Internet
Author: User

1. Renderbody does not have a "master page" in the Razor engine, instead a page called "Layout" (_layout.cshtml) is placed in the shared view folder. On this page, you'll see a statement in the tag: @RenderBody () It works like a server control in a master page, and when you create a view based on this layout page, the contents of the view are merged with the layout page. The contents of the newly created view are rendered between the labels through the @renderbody () method of the layout page. This method does not require parameters and can only occur once.

2. Renderpage from the name can guess this method is to render a page. For example, the fixed head of the Web page can be placed in a shared view file, and then in the layout page through this method call, the usage is as follows: @RenderPage ("~/views/shared/_header.cshtml") with parameter @RenderPage ("~ /views/shared/_header.cshtml ", new{parm=" my ", parm2=" You ") call page Get parameter://Get Renderpage () pass over parameter @PageData [" Param "]

3. RendersectionThe layout page also has the concept of section, which means that if a section is defined in a view template, it can be rendered separately, using the following: @RenderPage ("~/views/shared/_header.cshtml") @ Renderbody ()///template added a section @RenderSection ("Head") of course also to define the section in the view, otherwise an exception will occur: @section head{//do} to prevent exceptions due to missing sections, you can give Rendersect Ion () provides a 2nd parameter: @RenderSection ("submenu", false) or @if (issectiondefined ("submenu")) {@RenderSection ("S Ubmenu ", false)} else {<p>submenu section was not defined!</p>}
[email protected]The Partial creates its own TextWriter instance each time and caches the content in memory.  Finally, the content of all writer output is sent to a Mvcstring object more often we use @{html.renderpartial ("Details");} And not @html.partial.

The difference between renderpage () and renderpartial ()

A page called by Renderpage () can only use it to pass past data. RenderPartial () can use data such as Viewdata,model.

The difference between html.renderpartial and html.renderaction

Html.renderpartial is suitable for use in repeated use of the UserControl, and only need to show the content through the model, or for advertising UserControl is also suitable. Html.renderaction will call the Controller's action method first, if this UserControl need to get data through the database to render (through action to read the database), it is more appropriate to use this method.

Http://www.cnblogs.com/yeagen/archive/2013/02/21/2921279.html

The roles and differences of @RenderBody, @RenderSection, @RenderPage, Html.renderpartial, html.renderaction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.