how do i access fields from items in a list

Topics: Customizing Orchard, Writing modules
Aug 23, 2011 at 11:08 PM

I have created a container widget that contains a list of custom content parts and i can see the list through shape tracing, what i want to do is loop through the list to render each item using a custom template. I cant seem to access the fields on the List Items in my template.

The list contains content parts with an ImagePickerField called Image - how do i access that field?

Ive tried this but it doesnt work...

@{
    IEnumerable<object> items = Model.ContentItems;
    
    foreach(var item in items)
    {
        @Html.Raw(item.Image.Url)
    }
}
Any help appreciated as usual.
Coordinator
Aug 23, 2011 at 11:50 PM

You need to go through the part, and then the field. So if the field in on part Foo, you'd do something like item.Foo.Image.Url.

Oct 3, 2011 at 9:21 PM

Kinda piggy-backing on this, since it's the same subject... what if I need to filter out the items before they ever get rendered? The container filtering doesn't really help, as the filter parameter is part of the slug. So the goal here is just to go through all of Model.ContentItems and only render the ones that match... my code is as follows:

    IEnumerable<object> itms = Model.ContentItems;
    List<object> os = new List<object>();
    foreach(var i in itms){
        if (i.AssetItem.Category.SelectedValue.ToLower() == category){
            os.Add(i);
        }
    }

where AssetItem is my content Type, and Category is the field I need to get at. What am I missing?

Coordinator
Oct 5, 2011 at 8:29 PM

I don't know: you didn't say how it doesn't work.

Oct 5, 2011 at 8:36 PM

Sorry, it doesn't work at all. Just errors out here:

if (i.AssetItem.Category.SelectedValue.ToLower() == category) {

with the typical "
CS1061: 'object' does not contain a definition for 'AssetItem' and no extension method 'AssetItem' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)" message.
Coordinator
Oct 5, 2011 at 9:08 PM

Ah, well, yes, you cast the content items to an enumeration of objects, and then try to access the AssetItem property of one of those. Object has no idea what you're asking for here. You'll have to cast it into something else first. dynamic comes to mind.

Oct 5, 2011 at 10:26 PM

I tried dynamic too, and it had similar issues. I guess if I just had a better understanding of what Model's model is at any given point of the process, that might give me enough to figure this out...

Coordinator
Oct 5, 2011 at 11:00 PM

Model is the shape. So if a controller action created the shape, you should be able to peek in there and find out. You can also attach a debugger and find out. You can also enable shape tracing and dig into the model from the model tab.

Oct 6, 2011 at 3:21 PM

What about when the content type is something created in the CMS rather than through a custom module? I've been trying to code field types rather than content types, so the guy managing the site can more easily understand everything.

Coordinator
Oct 6, 2011 at 8:14 PM

What about it? Accessing fields on custom types should be no different.