I. _layout.cshtml master page under the shared folder, views folder
@RenderBody
When you create a view that is based on a _layout.cshtml layout page, the contents of the view are merged with the layout page, and the content of the newly created view is passed through the @renderbody () of the _layout.cshtml layout page Methods are rendered between labels.
@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"]
@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
To prevent exceptions due to a missing section, you can provide a 2nd parameter to Rendersection ():
@RenderSection ("Head", false)
Or
@if (issectiondefined ("Head"))
{
@RenderSection ("Head", false)
}
Else
{
<p>submenu section was not defined!</p>
}
The code is as follows:
- <! DOCTYPE HTML>
- <html>
- <head>
- <title> @ViewBag title</title>
- <link href="@Url. Content (" ~/content/site.css ")" rel="stylesheet" type=" Text/css " />
- <script src="@Url. Content (" ~/scripts/jquery-1.4.4.min.js ")" type="Text/javascript" ></script>
- @RenderSection ("Head", required:true) @*view page Custom specific js/css use *@
- </head>
- <body>
- @RenderPage ("~/views/shared/_header.cshtml")
- @RenderBody ()
- </body>
- </html>
Second, create the view, use the master page
The code is as follows:
- @{
- viewbag.title = "Index";
- Layout = "~/views/shared/_layout.cshtml";
- }
- <h2>index</h2>
- @section head{
- <script type="Text/javascript">
- $ (function () {
- Alert ("Hello jquery");
- });
- </Script>
- }
- <p> Execute C # normal syntax </P><br />
- @DateTime. Now.Date.ToShortDateString ()
- <p> Execute C # statement segment</P>
- @{
- List<string> list = new list<string> {"Mvc3", "Razor"};
- List. Add (". Net4 ");
- }
- <ul>
- @foreach (string s in list)
- {
- if (string. IsNullOrEmpty (s))
- {
- <li> empty </li>
- }
- Else
- {
- <li>@s</li>
- }
- }
- </ul>
Third, generate the source code of the page
<! DOCTYPE html>
<title>Index</title>
<link href= "/content/site.css" rel= "stylesheet" type= "Text/css"/>
<script src= "/scripts/jquery-1.4.4.min.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
$ (function () {
Alert ("Hello jquery");
});
</script>
<body>
<p> Execute C # common syntax </p><br/>
2013/3/11
<p> Execute C # statement segments </p>
<ul>
<li>Mvc3</li>
<li>Razor</li>
<li>. Net4</li>
</ul>
</body>
Iv. @Html. Partial
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");} And not @html.partial.
The difference between html.renderpartial () and @html.partial
Html.renderpartial output directly to the current HttpContext (because it is a direct output, so the performance is good).
Html.partial generates a string directly from the view content and returns (equivalent to having an escaped procedure).
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.
such as: @{html.renderpartial ("Basicchart", model);}
With this overload, you can use a strong type in a partial view, and then use the second parameter in the main master to pass the model past
@{html.renderpartial ("Basicchart", viewdata["MyData"]);}
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.
Add:
1. The return value of the method with render is void, output inside the method, and the return value type is mvchtmlstring, so this can only be used:
@Html. Partial corresponds to @{html.renderpartial (...);}
@Html. Action corresponds to @{html.renderaction (...);}
2, html.partial can directly provide the user control name as a parameter, and html.action need to have a corresponding action, within the action return Partailresult (that is, Retun Partialview ()).
3, for a simple user control without any logic, it is recommended to use html.partial; for user controls that need to set some model, Html.action is recommended. Of course, there are model data can also use the Html.partial method, you can see the overloads of the method.
4, the use of html.action has the advantage that can be based on different scenarios to choose a different user control.
Like what:
@Html. Action ("Userinfocontrol")
In the corresponding Userinfocontrol this action, when the user is not logged in, you can Retun Partialview ("Logonusercontrol"), after logging in, you can Retun Partialview (" Userinfocontrol ");
Reprint, Source address: http://www.cnblogs.com/xlhblogs/archive/2013/06/09/3129449.html
ASP. NET MVC Layout page template page How to use details