如何根据父节点的文档类型在 foreach 循环中以不同方式显示信息?
How do I display information in a foreach loop differently depending on the parent node's doc type?
所以我有一个 foreach 循环,它当前显示一个节点列表,然后是一个嵌套的节点列表。我想稍微扩展一下,以便嵌套列表根据其父节点的文档类型看起来有所不同。
这是我正在使用的层次结构:
- 目录(文档类型是目录登陆)
- 玩家(文档类型为玩家文件夹)
- 托尔金
- 刘易斯
- 当前角色(doctype 是玩家角色文件夹)
- 凯妮丝
- NPCs(doctype是NPC文件夹)
- 兰多
- 过去的字符(doctype 是过去的字符文件夹)
- 埃尔隆德
- 阿斯兰
所以我想要一个 if 语句,我想,基本上是问 "if the parent is Player Folder, display Name and Picture of each node beneath it, if it's Character Folder, display Name and Picture and Summary, if it's anything else, just display the name")。问题是我不知道如何让它检查它是什么文档类型。
这是我目前的代码:
<section id="directoryListing">
<ul>
@{
var LogsNode = Model.Content.AncestorOrSelf("DirectoryLanding");
}
@foreach (var node in LogsNode.Children.Where("Visible"))
{
<li>
<h2>@node.AsDynamic().Name</h2>
<ul>
@foreach (var childnode in node.Children)
{
<!-- If statement would presumably begin here -->
<li><!-- each section of the If statement would contain something like this-->
<a href="@childnode.Url">@childnode.Name</a>
</li>
<!-- If statement would presumably end here -->
}
</ul>
</li>
}
</ul>
</section>
动态版本应该有 DocumentTypeAlias 属性 或 NodeTypeAlias。
您可以在此处查看很棒的作弊 sheet(也适用于 v7):
https://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets
像这样:
<section id="directoryListing">
<ul>
@{
var LogsNode = Model.Content.AncestorOrSelf("DirectoryLanding");
foreach (var node in LogsNode.Children.Where("Visible"))
{
<li>
<h2>@node.Name</h2>
<ul>
@foreach (var childnode in node.Children)
{
if (childnode.Parent.DocumentTypeAlias == "Character")
{
<li>
<a href="@childnode.Url">@childnode.Name</a>
</li>
}
else if (childnode.Parent.DocumentTypeAlias == "Player")
{
<li>
<a href="@childnode.Url">@childnode.Name</a>
</li>
}
}
</ul>
</li>
}
}
</ul>
</section>
所以我有一个 foreach 循环,它当前显示一个节点列表,然后是一个嵌套的节点列表。我想稍微扩展一下,以便嵌套列表根据其父节点的文档类型看起来有所不同。
这是我正在使用的层次结构:
- 目录(文档类型是目录登陆)
- 玩家(文档类型为玩家文件夹)
- 托尔金
- 刘易斯
- 当前角色(doctype 是玩家角色文件夹)
- 凯妮丝
- NPCs(doctype是NPC文件夹)
- 兰多
- 过去的字符(doctype 是过去的字符文件夹)
- 埃尔隆德
- 阿斯兰
所以我想要一个 if 语句,我想,基本上是问 "if the parent is Player Folder, display Name and Picture of each node beneath it, if it's Character Folder, display Name and Picture and Summary, if it's anything else, just display the name")。问题是我不知道如何让它检查它是什么文档类型。
这是我目前的代码:
<section id="directoryListing">
<ul>
@{
var LogsNode = Model.Content.AncestorOrSelf("DirectoryLanding");
}
@foreach (var node in LogsNode.Children.Where("Visible"))
{
<li>
<h2>@node.AsDynamic().Name</h2>
<ul>
@foreach (var childnode in node.Children)
{
<!-- If statement would presumably begin here -->
<li><!-- each section of the If statement would contain something like this-->
<a href="@childnode.Url">@childnode.Name</a>
</li>
<!-- If statement would presumably end here -->
}
</ul>
</li>
}
</ul>
</section>
动态版本应该有 DocumentTypeAlias 属性 或 NodeTypeAlias。
您可以在此处查看很棒的作弊 sheet(也适用于 v7):
https://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets
像这样:
<section id="directoryListing">
<ul>
@{
var LogsNode = Model.Content.AncestorOrSelf("DirectoryLanding");
foreach (var node in LogsNode.Children.Where("Visible"))
{
<li>
<h2>@node.Name</h2>
<ul>
@foreach (var childnode in node.Children)
{
if (childnode.Parent.DocumentTypeAlias == "Character")
{
<li>
<a href="@childnode.Url">@childnode.Name</a>
</li>
}
else if (childnode.Parent.DocumentTypeAlias == "Player")
{
<li>
<a href="@childnode.Url">@childnode.Name</a>
</li>
}
}
</ul>
</li>
}
}
</ul>
</section>