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.