Umbraco 7: Get fields from same property based on current page

。_饼干妹妹 提交于 2020-01-25 04:24:10

问题


In my Content section I have a property editor (Archetype) that allows to set content for the site independently from the content tree. It looks like this:

I need to display only the sub categories from one category based on what page I'm currently on. What I have now is:

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

}  

This is displaying all the sub category items from all categories. It should display only the sub categories based on current page. How can I make the connection between current page and the sub category item? Or it is best to stick with the property editor in the content tree pages?


回答1:


The issue is not the code, but rather the structure of your Content Tree. You have nothing to Associate the Category/Offer to the page you want to display it on.

In order to create an the Association between ContentPage & Category/Offer DocType, you could try one of the following:

  1. Keep the Category/Offers as they are, independent of the content tree (i.e. Pool of Categories/Offers etc.) Then put a MNTP picker (multi-node tree picker) on the DocType that you want to create the Association between (i.e. 'ProductPage').

Each Page where you want to display the Category/Offer(s) you would need to call the MNTP Picker Property to get the NodeId of that specific Category/Offer for the current page.

Example:

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. Put your ArcheType property editor on the DocType you want to Associate with. Then simply call the property for that page and loop over the items again (see example below).

Example:

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

}  

Good Luck C



来源:https://stackoverflow.com/questions/25639424/umbraco-7-get-fields-from-same-property-based-on-current-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!