Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a contentpicker field value in Orchard

So I am using a Content Picker to display a list of selected items, and by default the out put looks like this:

Testimonial: View, View

But I would like it to actually show the parts of the ContentItem. This is the code that loops through the items:

@using Orchard.ContentPicker.Fields
@using Orchard.Utility.Extensions;

@{
    var field = (ContentPickerField) Model.ContentField;
    string name = field.DisplayName;
    var contentItems = field.ContentItems;
}

<p class="content-picker-field [email protected]()">
    <span class="name">@name:</span>
    @if(contentItems.Any()) {
        foreach(var contentItem in contentItems) {
            <span class="value"> @Html.ItemDisplayLink(contentItem)</span>
            if(contentItem != contentItems.Last()) {
                <span>,</span>
            }
        }
    }
    else {
        <span class="value">@T("No content items.")</span>
    }
</p>

I've tried several different ways of accessing the data but usually end up with warnings saying that it does not contain a reference, or that it can't convert it to a string.

I know that on the actual page for these items, they're in Model.ContentPart.Quote.Value


Edit I think the problem I am having is that I on the main page, I can't go to Models.Content.Quote or anything similar to access the data because its not listed on this page. The only data about each Quote thats listed is the ID.

like image 458
jassok Avatar asked Nov 13 '22 07:11

jassok


1 Answers

Simply change var to dynamic in foreach.

Therefore, you change this:

foreach(var contentItem in contentItems) {
            <span class="value"> @Html.ItemDisplayLink(contentItem)</span>
            if(contentItem != contentItems.Last()) {
                <span>,</span>
            }
        }

to this:

foreach(dynamic contentItem in contentItems) {
            <span class="value"> @Html.ItemDisplayLink(contentItem)</span>
            if(contentItem != contentItems.Last()) {
                <span>,</span>
            }
        }

Then you can access any part inside contentItem like this:

contentItem.ZenGalleryPart.Path


foreach(dynamic contentItem in contentItems) {
            <span class="value"> @Html.ItemDisplayLink(contentItem)</span>
            <span class="value"> @T(contentItem.ZenGalleryPart.Path)</span>
            if(contentItem != contentItems.Last()) {
                <span>,</span>
            }
        }
like image 118
user3879870 Avatar answered Dec 01 '22 23:12

user3879870