Razor engine learning: renderbody, renderpage, and rendersection {turn}

Source: Internet
Author: User

ASP. net MVC 3 has been officially released. Now it is estimated that many people are studying hard, and I am not an exception. I just saw an article and introduced three very useful methods: renderbody, now let's summarize the renderpage and rendersection.

1. renderbody

Without the "Master page" in the razor engine, the "layout" Page (_ layout. cshtml) is placed in the shared view folder. On this page, the <body> tag contains the following statement:

@ Renderbody ()

In fact, its role is similar to the <contentplaceholder> Server Control on the master page. When you create a view based on the layout page, the view content is merged with the layout page, the content of the newly created view is displayed between the <body> labels through the @ renderbody () method of the layout page.

This method does not require parameters and can only appear once.

2. renderpage

We can guess from the name that this method is to render 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 ()

The code is concise and intuitive. In addition, it seems that the underlined View File has special meanings. Please study it later.

3. rendersection

The layout page also has the concept of section. That is to say, if a section is defined in a view template, it can be presented separately. The usage is as follows:

@ Renderpage ("~ /Views/shared/_ header. cshtml ")
@ Renderbody ()
@ Rendersection ("footer ")

Of course, you must define the section in the view. Otherwise, an exception occurs:

@ Section footer {
<B> footer here </B>
}

To prevent exceptions due to missing sections, You can provide rendersection () with 2nd parameters:

@ Rendersection ("footer", false)

Http://www.dotnetcurry.com/ShowArticle.aspx? Id = 636

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.