This article is a mirror article of machine translation, please click here to jump to the original article.

View: 13486|Reply: 0

[ASP.NET] @RenderBody、@RenderSection、@RenderPage、Html.RenderPartial、Html.RenderActi...

[Copy link]
Posted on 7/19/2015 11:37:53 PM | | |
1. RenderBody
In the Razor engine, there is no "master page", instead a page called "layout" (_Layout.cshtml) is placed in the shared view folder. On this page, you'll see a statement in the tag:
@RenderBody()
In fact, it works similarly to the server control in the master page, when a view based on this layout page is created, the contents of the view are merged with the layout page, and the content of the newly created view is rendered between the labels through the layout page's @RenderBody() method.
This method does not require parameters and can only appear once.
2. RenderPage
As you can guess from the name, this method is to present a page. For example, the fixed header in a web page can be placed separately in a shared view file, and then called in this method in the layout page, as follows:
@RenderPage(“~/Views/Shared/_Header.cshtml”)
With parameters
@RenderPage(“~/Views/Shared/_Header.cshtml”,new{parm="my",parm2="you")
Call the page to get the parameters:
Get the parameters passed by RenderPage().
@PageData["param"]
3. RenderSection
The layout page also has the concept of a section, that is, if a section is defined in a view template, it can be presented separately in the following way:
@RenderPage(“~/Views/Shared/_Header.cshtml”)
@RenderBody()
//模板里添加了一个节
@RenderSection(“head”)
Of course, also define the section in the view, otherwise an exception will occur:
@section head{
//do
}
To prevent exceptions due to missing sections, you can give RenderSection() a second argument:
@RenderSection("SubMenu", false)
or
@if (IsSectionDefined("SubMenu"))
{
@RenderSection("SubMenu", false)
}
else
{
<p>SubMenu Section is not defined!</p>
}
4.@Html.Partial
Partial creates its own instance of TextWriter each time and caches the content in memory. Finally, send all the writer output to an MvcString object
More often than not, we'll use @{ html.RenderPartial("details"); instead of @Html.Partial
The difference between RenderPage() and RenderPartial().
RenderPage() calls can only use it to pass past data.
RenderPartial() can use viewdata, model, and other data.
The difference between Html.RenderPartial and Html.RenderAction
Html.RenderPartial is suitable for repeated UserControls, and only needs to be used to render content through models, or for UserControls for advertisements. Html.RenderAction will first call the Controller's Action method, if this UserControl needs to obtain data from the database to render (read the database through the Action), this method is more suitable for this time.
5.Html.Partial("MyView")
Return an attempt stream as an MvcHtmlString, following standard routing rules.
Renders the "MyView" view to an MvcHtmlString. It follows the standard rules for view lookup (i.e. check current directory, then check the Shared directory).
Html.RenderPartial("MyView")

Similar to Html.Partial(), the difference is that it is typed directly into the page without caching. Does the same as Html.Partial(), except 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")

Returns a special view with paths, file names, etc., and outputs directly like Heml.RenderPartial() without caching. model variables can be passed. Renders the specified view (identified by path and file name rather than by view name) directly to the response stream, like 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 is more readable. Other than that there doesn't seem to be any differences functionality wise.





Previous:ASP.NET---- implement a click button or link to pop up the login dialog
Next:Set the CheckBoxList to not wrap the line
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com