Copied to clipboard

Flag this post as spam?

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


  • Signe 4 posts 34 karma points
    Dec 05, 2014 @ 12:05
    Signe
    0

    Randomize data from archetype

    Hi all

    I have a list of sponsor logos, that I need to randomize. The sponsors are created with archetype in Umbraco 7.1.9

    Right now my foreach looks like this:

    @foreach (var sponsor in @CurrentPage.AncestorOrSelf(1).GetPropertyValue<ArchetypeModel>("sponsors")) {
         <img src="@Umbraco.Media(sponsor.GetValue("sponsorLogo")).umbracoFile" alt="" />
    }
    

    Any ideas on how to do that?

    Even better if I can randomize and only pullout 3 sponsors.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 05, 2014 @ 13:12
    Dennis Aaen
    0

    Hi signe and welcome to our.

    I have worked with archetype before but, in Razor you have two methods Take and Random, so perhaps this will work for you

    @foreach(var sponsor in CurrentPage.AncestorOrSelf(1).GetPropertyValue<ArchetypeModel>("sponsors").Take(3).Random()){
         
    <img src="@Umbraco.Media(sponsor.GetValue("sponsorLogo")).umbracoFile" alt=""/>
    }

    Hope this helps,

    /Dennis

  • Signe 4 posts 34 karma points
    Dec 05, 2014 @ 14:24
    Signe
    0

    Hi Dennis

    The problem is the ArchetypeModel does not contain the methods Take() and Random(), so these cannot be used in this context.

    /Signe

  • jivan thapa 194 posts 681 karma points
    Dec 06, 2014 @ 20:37
    jivan thapa
    1

    Have you try using Model.Content instead of CurrentPage

    foreach (var sponsor in Model.Content.AncestorOrSelf(1).GetPropertyValue<ArchetypeModel>("sponsors").Take(3).RandomOrder())
        {
            <img src="@Umbraco.Media(sponsor.GetValue("sponsorLogo")).umbracoFile" alt="" />
        }
    
  • Signe 4 posts 34 karma points
    Dec 09, 2014 @ 09:51
    Signe
    0

    Hi Jivan

    I have tried that, and the Take() method works, but I still get an error telling me that the ArchetypeModel does not contain the Random() method (this is actually the most important).

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Dec 09, 2014 @ 09:58
    Dave Woestenborghs
    0

    can you try Take(3).ToList().Random().ToList()

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Dec 09, 2014 @ 10:00
    Dave Woestenborghs
    0

    And if that doesn't work you can always google on c# list random. You will find plenty of examples.

    Dave

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Dec 09, 2014 @ 10:03
    Dave Woestenborghs
    0

    I just had a look at the razor cheatsheet.

    In V6 MVC it's not called Random anymore but RandomOrder : http://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets

    dave

  • jivan thapa 194 posts 681 karma points
    Dec 09, 2014 @ 10:19
    jivan thapa
    100

    @signe, If you look carefully. It's not .Random(), it is

    .RandomOrder()
    
  • Signe 4 posts 34 karma points
    Dec 09, 2014 @ 10:27
    Signe
    0

    Oh, my mistake! Yeah it works now - thanks a lot :-)

  • jivan thapa 194 posts 681 karma points
    Dec 09, 2014 @ 11:13
    jivan thapa
    0

    :) @signe If your problem is solved, remember to mark as "solved".

Please Sign in or register to post replies

Write your reply to:

Draft