一、@RenderSection定義
HelperResult RenderSection(string name)
但是當如果使用了_Layout.cshtml做母版頁的頁沒有實現Section的話,就會拋出異常,這是因為在_Layout.cshtml中使用的是@RenderSection("SubName"),他要求所有子頁都要實現。
重載函數
HelperResult RenderSection(string name, bool required = true)
其中,required默認為true表示引用這個布局頁的所有View必須含有該Section,設為false則為可以有,也可以沒有。
二、@RenderSection使用示例
1、layout布局頁
HTML 代碼
復制
<body>
<div id="header">@{Html.RenderAction("Menu", "Global");}</div>
<div id="sideBar">
@RenderSection("SubMenu",false)
</div>
<div id="container">@RenderBody()</div>
<div id="footer">@{Html.RenderAction("Footer", "Global");}</div>
</body> 
2、添加一個About。cshtml,使用_Layout.cshtml布局頁
C# 代碼
復制
@{
ViewBag.Title = "About";
}
@section SubMenu{
Hello This is a section implement in About View.
}
