Asp.net MVC4 advanced programming learning notes-view learning lesson 3 Razor page layout 20171010, mvc400001010
Razor page layout
1) Use the @ RenderBody tag on the layout template page to render the main content. For example, for many web pages, the header and the tail are the same, and the @ RenderBody is used in the intermediate content to display different page content.
2) use @ RenderSection ("SectionName") on the layout template page to render the node content of SectionName.
Define a node Example:
@section SectionName{ This is the <strong>Foot page</strong>.}
By default, each page needs to provide the corresponding node definition for the layout. There is another overload method to enable this node on the page.
Example:
<foot>@RenderSection(“Foot”,required,false)</foot>
Another way is to use the default output content without defining the corresponding section.
Example:
@if(IsSectionDefined(“Foot”)){ RenderSection(“Foot”);}else{ <span>This is the default foot.</span>}
Note: The template delegate can better implement this method and will be learned later.
3) _ ViewStart. cshtml is used to specify a default layout. It is suitable for switching the layout when multiple templates are used. If a view requires a custom Layout, You can override the Layout value to change the Layout mode.
The following example code specifies the default layout page in the _ ViewStart. cshtml file:
@{ Layout = "~/Views/Shared/_Layout.cshtml";}
2) specify some views.
Example:
public ActionResult Message(){ ViewBag.Message = "This is a partial view"; return PartialView(); }
Note that ifMessage. CshtmlThe default layoutViewStartIf this parameter is specified, the layout page will not be rendered here unlessMessage. CshtmlSpecified inLayoutTo render the corresponding layout page.
4) Update the branch view using Ajax.
Load View B (message view) in view A. The Code of view A is Example:
<Div id = "result"> Test message </div> @ section scripts {<script type = "text/javascript" >$ (function () {$ ("# result "). load ("/home/message") ;}); </script>}
The basic knowledge of the view has come to an end and needs to be used flexibly in the future. Model will be learned later.