3
votes

In my Orchard site, there is a content type named Product. It has the parts Container and Routable. Products can contain a content type named ProductFeature.

I have overrode the view Content-Product.cshtml to modify the html when browsing to the Route url. Within this view, how can I get a list of all ProductFeature's the Product contains?

This post shows how to do this for a blog Widget. http://weblogs.asp.net/bleroy/archive/2011/03/27/taking-over-list-rendering-in-orchard.aspx

I'm having a hard time finding how to do this in the Product's content view. The code from the above example throws a null exception error, so the model structure must be different. I I tried looking at the model using Shape Tracing or debugging in Visual Studio, but couldn't find the contained items.

Any help would be appreciated.

3
Did you ever figure this out? I am attempting to do the same thing and cannot understand Bertrand Le Roy's answer.ihake

3 Answers

2
votes

If you make sure your placement.info includes the container part, then your list of features should already being displayed.

2
votes

There are two options that I use (I know Bertrand will call them ugly and unnecessary as he has worked hard to give us many other tools, but if you either haven't grasped the full beauty of Orchard and how ingenious it is, you need these hacks.)

So here are 2:

  1. Create a partial view Parts.Container.Contained-YourContentTypeName.cshtml

    inside:

    @foreach (var p in Model.List.Items)
    {
       // regular content parts, Title and Body
       string title = p.ContentItem.TitlePart.Title;
       string body = p.ContentItem.BodyPart.Text;
    
       // Your custome content parts
       string something = p.ContentItem.YourContentPartName.YourContentPartFieldName.Value;
    }
    
  2. in Content-YourContentTypeName.cshtml:

    var yourList = Model.Content.Items[1].List.Items;
    

At least it is Items[1] in my case, you can check with shape tracing in model where the list is. And from here on everything is the same as 1).

Hope it helps someone who stumbles upon tis thread, looking for this solution like I did few weeks back.

0
votes

To expand on Zoran's answer, if you don't want to hard-code the position of Container in the Items list you can use LINQ:

IEnumerable<dynamic> items = Model.Content.Items;
var container = (from item in items
                 where (item.ContentPart.PartDefinition.Name == "ContainerPart")
                 select item).ElementAt(0);

...

@Display(container.List.Items)