asp.netorchardcmsorchardcms-1.8

How do you get just the image URL in Orchard CMS?


I have made a "Team" content type and have a list page and a profile page. I have 3 custom fields for Position, Bio and Profile Image. The single profile page is all working as I'd like.

What I'm wanting to get on the listing page is the profile image as a background on a div but can't for the life of me work out how its done. I can get the image to show as an element. Done this by adding:

<Match ContentType="Team">
    <Match DisplayType="Summary">
        <Place Fields_MediaLibraryPicker="Content:1"/>
    </Match>
</Match>

But struggling to work out how to just get the URL to the profile image so that I can put it as in inline style on the div as a background.

Any help would be much appreciated.

Thanks Anto


Solution

  • In the override View you can get the image like this:

    var url = Model.ContentItem.TeamPart.ProfileImage.MediaParts[0].MediaUrl;
    

    Notice that the TeamPart is the part that is created by attaching fields to your team content item. The ProfileImage is the name of your MediaLibraryPickerField field.

    EDIT

    Since at least Orchard 1.9 (not sure since when) you can do:

    var url = Model.ContentItem.TeamPart.ProfileImage.FirstMediaUrl;