【问题标题】:Umbraco 7: Get fields from same property based on current pageUmbraco 7:根据当前页面从同一属性中获取字段
【发布时间】:2014-10-27 15:57:50
【问题描述】:

在我的内容部分,我有一个属性编辑器(原型),它允许独立于内容树为站点设置内容。它看起来像这样:

我只需要根据我当前所在的页面显示一个类别的子类别。我现在拥有的是:

var catItems = Umbraco.Content(1123).categoryItem; //get the Set Content property editor from Content Section

foreach (var item in catItems)
{
    foreach (var sub in item.GetValue<ArchetypeModel>("subCatItem"))
    {
        <div class="tbl_dt">
            <p class="offerName">@sub.GetValue("offerName")</p>
            <p class="departurePort">@sub.GetValue("departurePort")</p>
        </div>
    }

}  

这是显示所有类别的所有子类别项目。它应该只显示基于当前页面的子类别。如何在当前页面和子类别项目之间建立联系?还是最好还是在内容树页面中坚持使用属性编辑器?

【问题讨论】:

  • catItems的类型是什么?它是否具有映射到关联页面的属性(应该是 IPublishedContent)。然后,您可以使用该属性检查 item 是否与您的当前页面相关联。
  • 其实catItems的类型是DynamicPublishedContent。所以我应该向它添加一个内容选择器并将其映射到内容页面?如果是这样,我将如何检查该项目是否与当前页面相关联?

标签: asp.net asp.net-mvc razor umbraco umbraco7


【解决方案1】:

问题不在于代码,而在于内容树的结构。 您没有任何东西可以将类别/优惠与您要显示的页面相关联。

为了在 ContentPage 和 Category/Offer DocType 之间创建关联,您可以尝试以下方法之一:

  1. 保持类别/优惠不变,独立于内容树(即类别/优惠池等) 然后在要创建关联的 DocType 上放置一个 MNTP 选择器(多节点树选择器)(即“ProductPage”)。

您要显示类别/优惠的每个页面,您需要调用 MNTP Picker 属性来获取该特定类别/优惠的 NodeId 当前页面。

示例:

var catNodeId = @CurrentPage.pickerPropertyAliasHere; //this will give us the NodeId of that specific offer selected for the current page, may need to refactor your code to handle parsing etc.

var catItems = @Umbraco.Content(catNodeId).categoryItem; //Get Categories/Offers from the Offer picked for the current page.

foreach (var item in catItems)
{
    foreach (var sub in item.GetValue<ArchetypeModel>("subCatItem"))
    {
        <div class="tbl_dt">
            <p class="offerName">@sub.GetValue("offerName")</p>
            <p class="departurePort">@sub.GetValue("departurePort")</p>
        </div>
    }

}
  1. 将 ArcheType 属性编辑器放在要关联的 DocType 上。 然后只需调用该页面的属性并再次遍历项目(参见下面的示例)。

示例:

var catItems = @CurrentPage.categoryItem; //Get Categories/Offers for the current page

foreach (var item in catItems)
{
    foreach (var sub in item.GetValue<ArchetypeModel>("subCatItem"))
    {
        <div class="tbl_dt">
            <p class="offerName">@sub.GetValue("offerName")</p>
            <p class="departurePort">@sub.GetValue("departurePort")</p>
        </div>
    }

}  

祝你好运 C

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-28
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多