Copied to clipboard

Flag this post as spam?

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


  • Jamie Bryant 3 posts 84 karma points
    Jul 04, 2019 @ 14:57
    Jamie Bryant
    0

    Media picker in nested content

    Im trying to get a media picker to work in a nested content. Its a simple nested content but i just cant seem to get the image to display. my code is:

     var items = Model.Value<IEnumerable<IPublishedElement>>("hrDoc");
    foreach (var item in items)
    {
        var name = item.Value<string>("sectionTitle");
        var image = item.Value<IPublishedContent>("media");
    
        <h1>@name</h1>
    
        if (image != null)
        {
    
            <a href="@image.Url"> </a>
        }
    }
    

    it displays the title but item only seems to have sectionTitle when i debug. Items has both sectionTitle and media.

    if anyone can help that would be great. Thank you

  • Jonathan Distenfeld 105 posts 618 karma points
    Jul 05, 2019 @ 10:13
    Jonathan Distenfeld
    100

    Hi Jamie,

    Are you sure it is a single media picker?

    If you want to display images from multiple media picker, you could try getting an Enumerable of images:

    var images = item.Value<IEnumerable<IPublishedContent>>("media");
    
    <h1>@name</h1>
    @foreach(var image in images)
    {
        if (image != null)
        {
            <a href="@image.Url"> </a>
        }
    }
    

    ~ Jonathan

  • Jamie Bryant 3 posts 84 karma points
    Jul 05, 2019 @ 10:28
    Jamie Bryant
    1

    Thank you that worked however i put the if before the foreach as i was getting a null exception as the foreach before that there is no image only the last count. which is set in my umbraco content. Thank you for this!

Please Sign in or register to post replies

Write your reply to:

Draft