MVC-Razor视图布局页的使用

1.在shared文件夹点右键,新建项,左边选择Razor,右边选择Layout Page

MVC-Razor视图布局页的使用

2.布局页里面有两种坑,RenderBody和RenderSection,RenderBody只能有一个,RenderSection可以有多个,参数required如果为true则必须在页面上进行填充,为false可以不填充

<!DOCTYPE html>
<html>
<head>
    <title>@Page.Title</title>
    @RenderSection("head", required: false)
</head>
<body>
    <table>
        <tr>
            <th>首页</th>
            <th>我的博客</th>
            <th>工具</th>
            <th>选项</th>
        </tr>
    </table>
    @RenderBody()

 @RenderSection("foot",true)
</body>
</html>

 

/*****************************************************************************************/


@{
    ViewBag.Title = "ShowLayoutPage";
    Layout = "~/Views/Shared/MainLayoutPage.cshtml";
}

<h2>ShowLayoutPage</h2>

@section head
{
    <script>
        onload = function myfunction() {

        };
    </script>
    
}

@section foot
{
    copyright
    
    }