Asp.net MVC4 advanced programming learning notes-view learning lesson 3 Razor page layout 20171010, mvc400001010

Source: Internet
Author: User

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.

Related Article

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.