5

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 content-picker-field-@name.HtmlClassify()">
    <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.

4

3 回答 3

1

只需更改vardynamicin foreach

因此,您更改此:

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

对此:

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

然后你可以contentItem像这样访问里面的任何部分:

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>
            }
        }
于 2014-10-12T06:50:40.820 回答
0

在以下示例中,我有一个 ContentType运动员,它使用 ContentPickerField 来选择 ContentType Team

在我为运动员创建的摘要视图中,我可以使用以下代码访问使用 ContentPickerField 选择的团队中的所有部分和字段。

内容运动员.Summary.cshtml

@using Orchard.Utility.Extensions;
@using Orchard.ContentPicker.Fields
@{    
    var teamField = (ContentPickerField) Model.ContentItem.Athlete.Team;    
    //make sure your var is cast (dynamic) and you will have access to all parts/fields
    var team = (dynamic)teamField.ContentItems.First();  
}

@team.TeamPart.City.Value
于 2014-02-18T01:15:36.817 回答
0

您基本上需要的是将内容项(在内容选择器中选择)转换为形状,以避免跳过重要的方面,如形状交替

幸运BuildDisplay()的是在视图中可用,所以你可以简单地写

foreach (var contentItem in contentItems)
{
  @Display(BuildDisplay(contentItem))
}

BuildDisplay()将为该形状创建一个形状contentItemDisplay()渲染该形状。

如果您需要部分的特定输出,contentItem那么您应该创建一个适当的形状替代。

于 2017-02-28T16:32:38.037 回答