代码之家  ›  专栏  ›  技术社区  ›  KlydeMonroe

Umbraco-仅获取文档类型的子元素

  •  1
  • KlydeMonroe  · 技术社区  · 9 年前

    我下面有一个结构链接

    第1页

    • --列项目一

      --列项目2

      --列项目三

      --列项目四

    第二页

    • --列项目OneB

      --列项目TwoB

    我有一个部分视图,我想显示每个子列项目,但目前我使用的是后代,它返回所有6个项目,而不是第1页上的4个和第2页上的2个。

    我的代码是

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        var root = Model.Content;
        var tiles = root.Descendants("tiles");
    
    
    
        if(tiles.Count() > 0)
        {
            <div class="row tile-row">
                @foreach(var node in tiles)
                {
                    <div class="col-md-3">
                        <div class="tile">
                            <h3>@(node.GetPropertyValue("tileTitle"))</h3>
                            @(node.GetPropertyValue("tileBodyText"))<br/>
                            <a class="btn btn-more" href="@(node.GetPropertyValue("tileButtonLink"))">@(node.GetPropertyValue("tileButtonText"))</a>
                        </div>  
                    </div>
                }
            </div><!--/.row-->
        }
    }
    

    如果我将后代更改为Children(),我会得到一个错误页。

    塔斯克

    1 回复  |  直到 9 年前
        1
  •  3
  •   Mivaweb    9 年前

    如果从 PageOne 或者从您的 PageTwo ,如果使用强类型对象,则可以执行以下操作:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        // Get this PageOne or PageTwo object
        var page = Model.Content;
    
        // Get the column node that is descendant of this page
        var column = root.Descendants("columnAlias");
    
        // Get all children of the column node that are published
        var childs = column.Children.Where(x => x.IsVisible());
    
        if(childs.Count() > 0)
        {
            <div class="row tile-row">
                @foreach(var node in childs)
                {
                    <div class="col-md-3">
                        <div class="tile">
                            <h3>@(node.GetPropertyValue("tileTitle"))</h3>
                            @(node.GetPropertyValue("tileBodyText"))<br/>
                            <a class="btn btn-more" href="@(node.GetPropertyValue("tileButtonLink"))">@(node.GetPropertyValue("tileButtonText"))</a>
                        </div>  
                    </div>
                }
            </div><!--/.row-->
        }
    }