@ Renderbody, @ rendersection, @ renderpage, HTML. renderpartial, HTML. renderaction

Source: Internet
Author: User

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, you will see the following statement in the tag:
@ Renderbody ()
In fact, this function is similar to the server control in the master page. When you create a view based on this layout page, the view content is merged with the layout page, the content of the newly created view is displayed between 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 ")
With Parameters
@ Renderpage ("~ /Views/shared/_ header. cshtml ", new {parm =" my ", parm2 =" you ")
Call the page to obtain parameters:
// Obtain the parameters passed by renderpage ()
@ Pagedata ["Param"]

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 ()
// Add a section to the template
@ Rendersection ("head ")
Of course, you must define the section in the view. Otherwise, an exception occurs:
@ Section head {
// Do
}
To prevent exceptions due to missing sections, You can provide rendersection () with 2nd parameters:
@ Rendersection ("submenu", false)
Or
@ If (issectiondefined ("submenu "))
{
@ Rendersection ("submenu", false)
}
Else
{
<P> submenu section is not defined! </P>
}

4.@Html.Partial
Each time partial creates its own textwriter instance and caches the content in the memory. Finally, it sends all the output content from writer to an mvcstring object.
In more cases, we will use @ {html. renderpartial ("details");} Instead of @ html. Partial.

Differences between renderpage () and renderpartial ()

The page called by renderpage () can only use it to pass data in the past.
Renderpartial () can use viewdata, model, and other data.

Differences between HTML. renderpartial and HTML. renderaction

Html. renderpartial is applicable to the usercontrol that is used for re-overwriting. It only needs to present the content through the model, or is also suitable for the usercontrol of the advertisement. Html. renderaction will call the Controller's action method first. If this usercontrol needs to be presented through the database (read the database through the action), this method is more suitable.

5.Html.Partial("Myview")

Return the attempted stream in the form of mvchtmlstring, following the standard routing rules.

Renders the "myview" view toMvchtmlstring. It follows the standard rules for view Lookup (I. e. Check current directory, then checkSharedDirectory ).

Html.Renderpartial("Myview")

AndHtml. Partial () is similar. The difference is that the request is directly input to the page without caching.

Does the sameHtml. Partial (), Could t that it writes its output directly to the response stream. This is more efficient, because the view content is not buffered in memory. However, because the method does not return any output,@ Html. renderpartial ("myview ")Won't work. You have to wrap the call in a code block instead:@ {Html. renderpartial ("myview ");}.

Renderpage("Myview. cshtml")

Return a special view with a path or file name, which is output directly like heml. renderpartial () without caching. You can pass the model variable.

Renders the specified view (identified by path and file name rather than by view name) directly to the response stream, likeHtml. renderpartial (). You can supply any model you like to the view by including it as a second parameter

 
Renderpage("Myview. cshtml",Mymodel)

I prefer

@ Renderpage("_ Layoutheader. cshtml")

Over

 
@{Html.Renderpartial("_ Layoutheader");}

Only because the syntax is easier and it is more readable. Other than that there doesn't seem to be any differences functionality wise.

 
 

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.