Copied to clipboard

Flag this post as spam?

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


  • Gísli Freyr Svavarsson 43 posts 126 karma points
    Sep 07, 2015 @ 11:15
    Gísli Freyr Svavarsson
    0

    5 Random items from Multiple media picker

    Hi people.. I'm trying to select 5 random photos from a multiple media picker in a random order as well. I've been trying to add .Random(5) at the for each loop but I always seem to get an error running the Marcro.

    My code works well without the Random but crashes with it.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @if (Model.Content.HasValue("loaderImages", true))
    {
        var imagesList = Model.Content.GetPropertyValue<string>("loaderImages", true).Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
        var imagesCollection = Umbraco.TypedMedia(imagesList).Where(x => x != null);
        var imgCount = 0;
    
        <div class="loader">
            <div class="loader-inner">
                <div class="pulse">
                    @foreach (var imageItem in imagesCollection) {      
                        imgCount++;
                        <div class="double-bounce-@imgCount">
                            <img src="@imageItem.Url" alt="@imgCount"/>
                        </div>              
                    }
                </div>
            </div>
        </div>
    }
    

    Hopefully someone is able to help me...

    mvh

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Sep 07, 2015 @ 11:37
    Dan Diplo
    101

    Could you try:

    var imageItem in imagesCollection.RandomOrder().Take(5)
    

    This works in 7.2.x

  • Gísli Freyr Svavarsson 43 posts 126 karma points
    Sep 07, 2015 @ 12:04
    Gísli Freyr Svavarsson
    0

    Awesome Dan.. Thank you that works great.

  • 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