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

Umbraco - Only Get Child Elements of an Document Type

我在

下面有一个结构link

第一页

第二页

我有一个部分视图,我想显示每个子列项目,但目前我正在使用后代,它返回所有 6 个项目,而不是 PageOne 上的 4 个和 PageTwo 上的 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(),我会收到一个错误页面。

谢谢

如果您从 PageOnePageTwo 调用分部视图,那么如果您使用的是强类型对象,则可以执行以下操作:

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