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

Source: Internet
Author: User

1. Renderbody
There is no "master page" in the Razor engine, instead a page called "Layout" (_layout.cshtml) is placed in the shared view folder. In this page, you will see a statement in the label:
@RenderBody ()
In fact, it works like a server control in a master page, when you create a view based on this layout page, the contents of the view are merged with the layout page, and 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, use the following:
@RenderPage ("~/views/shared/_header.cshtml")
With parameters
@RenderPage ("~/views/shared/_header.cshtml", new{parm= "my", parm2= "You")
Invoke page Get Parameters:
Gets the arguments passed by the Renderpage ().
@PageData ["Parm"]
3. Rendersection
The 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 ()
A section is added to the template.
@RenderSection ("Head")
Of course, you also define the section in the view, otherwise an exception will occur:
@section head{
Do
}
To prevent exceptions due to a missing section, you can provide a 2nd parameter to Rendersection ():
@RenderSection ("submenu", false)
Or
@if (issectiondefined ("submenu"))
{
@RenderSection ("submenu", 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 contents of all writer outputs are sent to a Mvcstring object.
More often we will 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.

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.