检查 Umbraco 节点长度

Checking Umbraco node length

我有一个面板,它是通过我创建的职位空缺页面创建和填充的。我是这样做的:

@{
    var root = CurrentPage.AncestorOrSelf(1);
    var newsNode = root.Descendants("News").First();
    var vacanciesNode = root.Descendants("Vacancies").First();
    string shortenedSummary = string.Empty;
}

<ul>
    @foreach (var vacancyItem in vacanciesNode.Descendants("Vacancy").Take(3).OrderBy("postDate desc"))
    {
        <p>here we are 2</p>
        @vacanciesNode.Count().ToString()
        <li>
            <h4><a href="@vacancyItem.Url">@vacancyItem.jobTitle</a></h4> <span>Posted on @vacancyItem.postDate.ToString("dd/MM/yyyy")</span>
            <p>
                @if (vacancyItem.jobSummary.Length <= 182)
                {
                    @vacancyItem.jobSummary
                }
                else
                {
                    shortenedSummary = vacancyItem.jobSummary.Substring(0, 182) + "...";
                    @shortenedSummary
                }
            </p>

            <a href="@vacancyItem.Url" class="btn btn-purple">Read More..</a>
        </li>
    }

</ul>

但是,当没有空缺项目时,我的列表是空的。如果是这种情况,我希望它显示为 "sorry no vacancies just now",但我不知道如何检查我的 vacanciesNode 中是否有任何项目。

有人可以告诉我如何实现吗?

由于 .Descendants() 方法 returns a DynamicContentList (一个集合)你可以简单地对集合做一个 .Count() 并检查它是否大于或等于到 1.

如果集合中的项目超过 0 个,则不为空。

所以,你需要做的是用一个 @if 语句围绕你的 @foreach 来检查这个,然后是一个 else 语句打印任何 html如果没有空缺你想显示

@if( vacanciesNode.Descendants("Vacancy").Take(3).OrderBy("postDate desc").Count() > 0) {
   //Do foreach
}
else
{
   //Write message about missing vacancies
}