问题
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:
- 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>
}
}
- 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