在MVC/Razor语法中,我试图理解为什么我们需要@RenderBody
.
例如(代码取自示例)
<html>
<head>
<meta charset="utf-8" />
<title>My WebSite</title>
<style>
#container { width: 700px; }
#left { float: left; width: 150px; }
#content { padding: 0 210px 0 160px; }
#right { float: right; width: 200px; }
.clear { clear: both; }
</style>
</head>
<body>
<div id="container">
<div id="left">
@RenderSection("left", required:false)
</div>
<div id="content">
@RenderBody()
</div>
<div id="right">
@RenderSection("right", required:false)
</div>
<div class="clear"></div>
</div>
</body>
</html>
@{
Layout = "~/_3ColLayout.cshtml";
}
<h1>Main Content</h1>
@section left {
<h1>Left Content</h1>
}
@section right {
<h1>Right Content</h1>
}
为什么我不能简单地@RenderSection
用于一切,像这样:
<div id="content">
@RenderSection("Body", required:true)
</div>
@section Body{
<h1>Body Content</h1>
}