How do I access values of a contentpicker item in a view?

Topics: Customizing Orchard, General, Troubleshooting
Jun 26, 2013 at 5:01 PM
I've created a custom content type called MediaImage and that type has a title body and a media picker field. Using the content picker, a user can added multiple "MediaImage" content items to a MediaSet.

When iterating through the "contentItems", I use this to get the image:
contentItem.Parts.SelectMany(p => p.Fields).First(f => f.Name == "Image").Storage.Get<string>(null)
And this to get the title:
@Html.ItemDisplayText(contentItem)
But in my view there does not seem to be a way to access the "bodypart" of the content item. How can this be done?
Coordinator
Jun 26, 2013 at 6:30 PM
For that first one, you'd probably get the same result with:
dynamic ci = contentItem;
var imageUrl = ci.MediaImage.Image.Url;
To get the body part, try this:
@Html.Raw(ci.BodyPart.Text)
Jun 27, 2013 at 12:47 PM
Bertrand-Thanks for the reply but this isn't working. I'm getting an error message when trying this:
dynamic ci = contentItem;
Line 21:             <span>@ci.MediaImage.Image.Url</span>

'Orchard.ContentManagement.ContentItem' does not contain a definition for 'MediaImage'
Also, when debugging this in VS "contentItem" does not seem to contain "MediaImage" anywhere in the object. The same thing happens when trying to get the value for BodyPart. They just don't seem accessible.

Please help.
Jun 27, 2013 at 11:20 PM
Make sure that your content item actually has a part called "MediaImage".
Check the same thing for "BodyPart".

Keep in mind that when working with dynamic objects, you don't always see what's in there. In the case of accessing parts and fields on a dynamically typed content item, you won't see the parts and their fields as properties. Instead, inspect the strongly typed contentItem variable and make sure the parts and fields are there.