简体   繁体   中英

Umbraco 7: Map property to page and check if is associated with current page

In my Content section I have a property editor (Archetype) that allows to set content for the site independently from the content tree. I need to display only the sub categories from one category based on what page I'm currently on. What I have now is:

//get the content with id of 1123 from Content Section, type DynamicPublishedContent
var catItems = Umbraco.Content(1123).categoryItem; 

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

See this reference for other details: Umbraco 7: Get fields from same property based on current page

Q: How can I map the property to a content page and check if is associated with current page and display only the fields with mapped current page? Can this be done by adding a content picker to it? If so how could I check if it is associated with current page?

First it is not good practice to reference content in code by their id. Rather use the document type alias which is guaranteed not to get deleted by a user.

Now to check for the existence of a property on the current page all you need to do is

@if (CurrentPage.HasValue("subCatItem"))
{
   string propertyStoredValue = CurrentPage.subCatItem.ToString();
}

Where "subCatItem" is the alias of the property you are checking for. The type of the property is not relevant in this case and bear in mind if the property is not mandatory and has not been given a value the above statement will evaluate to false even though the property exists on the document type (makes sense?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM