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

Source: Internet
Author: User

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

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 ["Param"]

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");} instead of @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.

5.Html.Partial("MyView")

Returns an attempt stream in the form of mvchtmlstring, followed by a standard routing rule.

Renders the "MyView" view to an MvcHtmlString . It follows the standard Rules for view lookup (i.e. check with current directory and then check the Shared directory).

Html.RenderPartial("MyView")

与Html.Partial()类似,区别是直接输入到页面,不进行缓存。

Does Html.Partial() the same as, except that it writes its output directly to the response stream. This was more efficient, because the view content was 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")

返回带路径、文件名等的特殊视图,同Heml.RenderPartial()一样直接输出,不进行缓存。可以传递model变量。

Renders the specified view (identified by path and file name rather than by view name) directly to the response stream, Li Ke Html.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's more readable. Other than, there doesn ' t seem to is any differences functionality wise.

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.