mvc中@RenderSection()研究 转载https://www.cnblogs.com/rrxc/p/4062827.html

一、@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.
}
posted @ 2019-05-31 10:01  DarJeely  阅读(199)  评论(0编辑  收藏  举报