orchardcmsorchardcms-1.10

Displaying Container Part Items From Content Picker Field


I have a content type called DayTile which I have setup with a content picker field that is limited to HotelSection type. This type is a container for the Hotels type.

I want to render all the hotels in the HotelSection when the DayTile has that section picked. Is this possible?

From what I have tried so far, it does not seem that the HotelSection's contained items are accessible as a content picker field.


Solution

  • The selected content items are exposed by the ContentPickerFields ContentItems property (see Orchard.ContentPicker.Fields.ContentPickerField).

    To access this from a DayTile content item, let's say from a Razor template named "Content-DayTile.cshtml", you would do it like so:

    @{
        dynamic contentItem = Model.ContentItem; // How to access the content item depends on the context. In this case, we're in a Content shape template.
    
       // Assuming your picker field is named "HotelSections" and attached to your DayTile content type (which in reality means it's attached to an automatically created part called "DayTile").
    
       var picker = contentItem.DayTile.HotelSections;
       var hotelSectionContentItems = picker.ContentItems; // Typed as IEnumerable<ContentItem>.
    }
    
    <ul>
       @foreach(var hotelSectionContentItem in hotelSectionContentItems)
       {
          // You can now either access individual parts and fields of each hotel section content item and render it, or you can choose to build a shape for each item and display that. Here are examples:  
    
          <li>@hotelSectionContentItem.TitlePart.Title</li>
    
          // or: 
          // Build a content shape for the content item.
          var hotelSectionContentShape = BuildDisplay(hotelSectionContentItem, "Summary");
    
         // Render the shape.
         <li>@Display(hotelSectionContentShape)</li>
    }