加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 有没有办法用asp.net mvc Razor ViewEngine制作

发布时间:2020-12-16 03:43:23 所属栏目:asp.Net 来源:网络整理
导读:我有一个类似于以下的Page.cshtml(不起作用): @{ Layout = "../Shared/Layouts/_Layout.cshtml"; var mycollection = (ViewBag.TheCollection as IQueryableMyCollectionType);}h2@ViewBag.Title/h2content here@if (mycollection != null mycollection.Cou
我有一个类似于以下的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.

解决方法

我最终做了一些有点hacky的事情来让它工作我需要它.

在我的页面上我有:

@{
    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

如果有更好的方式我想知道.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读