为每个循环指定两个模型/数组?

时间:2015-03-14 21:06:25

标签: umbraco umbraco7

我知道每个循环通常都集中在一个阵列上,但我是Umbraco的新手,我想知道这是否可行?

我的代码如下:

<div>
    <div class="row">
        @foreach (var feature in homePage.CSSHomepages.Where("featuredPage"))
        {
            <div class="3u">
                <!-- Feature -->
                <section class="is-feature">
                    <a href="@feature.Url" class="image image-full"><img src="@feature.Image" alt="" /></a>
                    <h3><a href="@feature.Url">@feature.Name</a></h3>
                    @Umbraco.Truncate(feature.BodyText, 100)
                </section>
                <!-- /Feature -->
            </div>
        }
    </div>
</div>

目前显示的是一个精选页面,但我也试图同时显示来自&#34; HTMLHomepages&#34;的精选页面。

我尝试过以下代码无效:

<div>
    <div class="row">
        @foreach (var feature in homePage.CSSHomepages.Where("featuredPage") & homePage.HTMLHomepages.Where("featuredPage"))
        {
            <div class="3u">
                <!-- Feature -->
                <section class="is-feature">
                    <a href="@feature.Url" class="image image-full"><img src="@feature.Image" alt="" /></a>
                    <h3><a href="@feature.Url">@feature.Name</a></h3>
                    @Umbraco.Truncate(feature.BodyText, 100)
                </section>
                <!-- /Feature -->
            </div>
        }
    </div>
</div>

但正如我所料,我得到运行时错误。有什么建议吗?

1 个答案:

答案 0 :(得分:2)

您收到的运行时错误与umbraco无关。 你有一个&amp; -sign。这在剃刀语言中不存在。 你应该至少使用&&,这意味着AND。 但是在这种情况下,您不想使用AND运算符,而是使用OR运算符:||。如果您在if语句中检查某些内容,这一切都将成立。

在这里循环一个数组。这意味着你需要在循环它们之前连接两个arrarys。通常,您会从Umbraco API获得两个IEnumerable。要将两个IEnumerables连接在一起,您可以使用Concat (see MSDN)函数。

我会做什么:

@{
   var featureList = homePage.CSSHomepages.Where("featuredPage").Concat(homePage.HTMLHomepages.Where("featuredPage"))
}

<div class="row">
  @foreach( var feature in featureList) {
     // your existing code
  }
</div>
相关问题