Differences between renderbody, renderpage, and rendersection in mvc3

Source: Internet
Author: User
<body>     @RenderBody() </body>

Without the "Master page" in the Razor engine, the "Layout" Page (_ Layout. cshtml) is placed in the shared view folder. On this page, you will see a statement in the <body> tag: @ RenderBody ()

ActuallyIts role is similar to the <contentplaceholder> Server Control on the master page.When you create a view based on this layout page, the content of the view is merged with the layout page, and the content of the newly created view is directed to the @ RenderBody () of the layout page () the method floated between <body> labels.

This method does not require parameters and can only be presented once.

 

<body>     @RenderPage("~/Views/Shared/_Header.cshtml")
@RenderBody() </body>

You can guess from the name.This method is to create a page. For example, a Fixed Header in a webpage can be separately placed in a shared view file, and then called through this method in the layout page. The usage is as follows:

@ RenderPage ("~ /Views/Shared/_ Header. cshtml ")

@ RenderBody ()

 

<body>     @RenderPage("~/Views/Shared/_Header.cshtml")
@RenderBody()
@RenderSection("footer") </body>

The layout page also has the concept of Section,Used to display a section defined in the view template individuallyThe usage is as follows:

@ RenderPage ("~ /Views/Shared/_ Header. cshtml ")

@ RenderBody ()

@ RenderSection ("footer ")

Define the section in the view. Otherwise, an exception occurs.

@section footer {
<b>Footer Here</b>
}

To prevent exceptions due to lack of sections, You can provide 2nd parameters to RenderSection:

@ RenderSection ("footer", false)

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.