那么在MVC3中使用布局,如果我想在页面级别上指定是否显示特定部分,最好的方法是什么?考虑以下页面:
@{
ViewBag.Title = "...";
Layout = "~/Views/Shared/Layout/_Layout.cshtml";
}
@section LetsBeFriends {
}
@section Header {
....
}
@section Body {
....
}
为了让“LetBeFriends”部分具有条件性,我已经按照以下方式实现了布局:
@{
if (IsSectionDefined("LetsBeFriends"))
{
@RenderSection("LetsBeFriends")
@Html.Partial("_LetsBeFriends")
}
}
@RenderSection("Body")
这种做法似乎有些粗糙,因为LetsBeFriends将始终是一个空的部分,它只是一个条件来决定是否呈现局部内容。有更好的方法吗?