我有一个类似于以下的 Page.cshtml(不起作用):
@{
Layout = "../Shared/Layouts/_Layout.cshtml";
var mycollection = (ViewBag.TheCollection as IQueryable<MyCollectionType>);
}
<h2>@ViewBag.Title</h2>
content here
@if (mycollection != null && mycollection.Count() > 0)
{
@section ContentRight
{
<h2>
Stuff
</h2>
<ul class="stuff">
@foreach (MyCollectionType item in mycollection )
{
<li class="stuff-item">@item.Name</li>
}
</ul>
}
}
正如我所说,这不起作用。如果集合中没有任何内容,我不想定义该部分。有没有办法让这样的工作?如果没有,我的其他选择是什么?我对这个 Razor ViewEngine 很陌生。
编辑
在我的布局中,我有:
@if(IsSectionDefined("ContentRight"))
{
<div class="right">
RenderSection("ContentRight")
</div>
}
我不想要的是当该部分为空时输出的 div 。