Copied to clipboard

Flag this post as spam?

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


  • Peter van Geffen 8 posts 98 karma points
    7 days ago
    Peter van Geffen
    0

    Get an image (random) from a media folder

    Hi,

    What i wanted to achieve is to get a single image in a random order from a media folder (by id)

    This worked for in V7 (looking at previous projects), but now in V8 i'm stuck.

    @{
        var galleryAlbum = Umbraco.Media(1210);
        foreach(var image in galleryAlbum.Children.RandomOrder().Take(1)){
            <div class="parallax-window" data-parallax="scroll" data-image-src="@Url.GetCropUrl(image, "Hero")"></div>
        }
    }
    

    Could someone help me out?

  • Alex Skrypnyk 5365 posts 19802 karma points MVP 3x admin c-trib
    6 days ago
    Alex Skrypnyk
    100

    Hi Peter

    What about this approach:

    var galleryAlbum = Umbraco.Media(1210);
    var theList = galleryAlbum.Children.ToList();
    var randomImage = theList.ElementAt(new Random(DateTime.Now.Millisecond).Next(theList.Count()));
    
  • Peter van Geffen 8 posts 98 karma points
    6 days ago
    Peter van Geffen
    0

    Thank you very much! Exactly what i wanted .

Please Sign in or register to post replies

Write your reply to:

Draft