Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • ThomasBrunbjerg 86 posts 178 karma points
    Aug 16, 2017 @ 11:17
    ThomasBrunbjerg
    0

    Displaying images from multiple media picker in partial view

    I am currently creating a partial view that will display a specific image from a multiple media picker property. The code i have for my partial view right now is this:

    @{
        var selection = Umbraco.TypedContent(1090).Children()
                            .Where(x => x.IsVisible())
                            .OrderBy("CreateDate");
    }
    
    @foreach(var item in selection){
        <div class="col-sm-12 col-md-6">
                                    <a class="thumbnail fancybox" rel="" href="images/sommerhuse/3.jpg">
                                        <img class="img_wr" alt="" src="images/sommerhuse/3.jpg" />
                                    </a>
                                </div>
                                <div class="col-sm-12  col-md-6">
                                    <h5><strong>@item.GetPropertyValue("sommerhusOverskrift")</strong></h5>
    
                                    <p class="static-height">@item.GetPropertyValue("sommerhusKortBeskrivelse")</p>
                                    <a class="subpage_btn btn_top_offset" href="@item.Url">Tryk for at se Mere</a>
                                </div>
        <li>
            <a href="@item.Url">@item.Name</a>
        </li>
    }
    

    For the image source i want the first image in my multiple media picker.

    I am unsure how to accomplish this. Before i have used code like this to display an image from the media archive:

    @(Model.Content.GetPropertyValue<IPublishedContent>("backgroundImageBlogPage").Url)
    
  • Dan Diplo 1505 posts 5911 karma points MVP 4x c-trib
    Aug 16, 2017 @ 11:30
    Dan Diplo
    2

    You can get the first image in a multimedia picker like this:

    @(Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("backgroundImageBlogPage").First().Url)
    

    But I'd be very wary of doing this as you'll get an exception if there are no images selected. You might want to break code down:

    string backgroundUrl = null;
    
    var images = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("backgroundImageBlogPage");
    if (images != null && images.Any())
    {
        backgroundUrl = images.First().Url;
    }
    

    Then use backgroundUrl as the value for your background image URL.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies