ASP. net mvc uses RenderSection to render nodes

Source: Internet
Author: User

I have no time to do ASP. NET mvc exercises in a few days, so I am busy with secondary development of ERP. In my busy schedule, I think that MVC still has a lot of basic knowledge to support and understand. I remember having practiced the MVC master page _ Layout. cshtml http://www.cnblogs.com/insus/p/3380419.html rendering Body (RenderBody) function before. Exercise the RenderSection rendering node today.

In the Views \ Shared directory, create a Layout Page:

 

The name is _ LayoutPage1.cshtml:

 

Line #13 code:

@RenderSection(, required: ) 


The name is "MyName", and the second parameter is set to false. In this case, the MyName node is defined in the view and will be displayed. Otherwise, it will not be displayed.

Create a controller Default1Controller. cs:

 

Once the controller is created successfully, in MVC5 or a later version, the corresponding directory is automatically created under the Views directory with the Controller name. Create a view as follows:

 

Browsing result:

 

 

If we set the parameter of line #13 to true in _ LayoutPage1.cshtml:

@RenderSection(, required: )


It means that we only need to reference the Layout page view and define it:

* Node Code *


If not defined, the view displays an exception at runtime:

 

In fact, we have another method to judge in Layout page:

 

After such a modification, all views that reference the Layout page, whether or not the following code is defined:

* Node Code *

No exception occurs.

 

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.