ASP.NET MVC3 模板页的使用

时间:2023-03-09 08:27:51
ASP.NET MVC3 模板页的使用

占位符的使用:

下面是一个模板页 _Layout.cshtml

<!DOCTYPE html>
<html>
<head>
@RenderSection("headtop", false)
@RenderSection("head", false)
</head>
<body id="body">
@RenderBody()
@RenderSection("foot", false)
</body>
</html>

在模板 _Layout.cshtml里面  @RenderSection("headtop", false)  、 @RenderSection("head", false) 、 @RenderSection("foot", false)  就是一个占位符, 相当于定义了一个抽象的方法(渲染部分) ,供子类实现,第二参数为true必须子类必须实现,子类的实现方式:

子类实现 名字为"head" 的渲染部分 @section head{ @*渲染内容*@ } ,其他的以此类推。

@RenderBody()   渲染主要内容。

子页面 User.cshtml

@using System.Web.Mvc.Html
@{
Layout = "~/Views/Shared/_Layout.cshtml";@*网站前台模版*@
}
@section head{
RenderSection("head", false) 部分的渲染
}
<div class="contentWrap">
RenderBody() 的渲染
</div>
@section foot{
RenderSection("foot", false) 部分的渲染
}

注意:以上只是我个人的见解,方便理解,不一定正确