我有一个类似于以下内容的 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 非常陌生。
Edit
在我的布局中我有:
@if(IsSectionDefined("ContentRight"))
{
<div class="right">
RenderSection("ContentRight")
</div>
}
我不想要的是当该部分为空时输出的 div。
我最终做了一些有点古怪的事情来让它按照我需要的方式工作。
在我的页面上我有:
@{
Layout = "../Shared/Layouts/_Layout.cshtml";
var mycollection = (ViewBag.TheCollection as IQueryable<MyCollectionType>);
ViewBag.ShowContentRight = mycollection != null && mycollection.Count() > 0;
}
然后在我的布局中我有:
@if(IsSectionDefined("ContentRight") && (ViewBag.ShowContentRight == null ||ViewBag.ShowContentRight == true))
{
<div class="right">
RenderSection("ContentRight")
</div>
}
else if(IsSectionDefined("ContentRight"))
{
RenderSection("ContentRight")
}
如果定义了该部分,则必须呈现它,但如果没有内容,我不希望<div>
s
如果有更好的方法我想知道。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)