Copied to clipboard

Flag this post as spam?

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


  • Chris Heijnen 6 posts 26 karma points
    Mar 10, 2013 @ 09:46
    Chris Heijnen
    0

    Get single random node from MNTP

    Hi Guys,

    The title actually explains my issue but what i want is to display 1 Random node from multiple nodes i have selected with the MNTP. 

    With this code i'm getting all the node (id's) and returning the property's in it, so that part works fine. Just want to display 1 randomly.

    @{
    
        var topNode = Model.AncestorOrSelf(1);
    
        foreach (var reference in topNode.referencesPicker)
        {
    
            var referenceNode = Model.NodeById(reference.InnerText);
    
            <h3>@referenceNode.referenceName</h3>
            <span>@referenceNode.referenceOneliner</span>
            @referenceNode.referenceText
    
        }
    
     }

    Chris

  • gary 385 posts 915 karma points
    Mar 10, 2013 @ 10:26
    gary
    0

    Hi Chris

    foreach(var reference in topNode.referencesPicker).Random(1)

    should work for you.

    G

     

     

  • Chris Heijnen 6 posts 26 karma points
    Mar 10, 2013 @ 10:33
    Chris Heijnen
    0

    Hi Gary,

    I think that's not going to work. Getting syntax error on that. If i write it down this way:

     foreach (var reference in (topNode.referencesPicker).Random(1))

    I get this error: 'umbraco.MacroEngines.DynamicXml' does not contain a definition for 'Random' 

    any suggestions?

  • gary 385 posts 915 karma points
    Mar 10, 2013 @ 10:40
    gary
    0

    Ah

    The MNTP throws that one!

    Found this answer here which you should be able to adapt if it doesn't work directly - it explains how it works.

    http://our.umbraco.org/forum/developers/razor/22237-Random-uComponents-Multi-Node-Tree-Picker

    Hope it helps G

  • Chris Heijnen 6 posts 26 karma points
    Mar 10, 2013 @ 10:52
    Chris Heijnen
    0

    Hi Gary,

    I already looked at that solution but i couldn't figure it out. Now with a fresh look i made it work! 

    This is the working code btw:

    @{
    var topNode = Model.AncestorOrSelf(1);
    
        var nodes = new DynamicNodeList();
    
        foreach(var referenceItem in topNode.referencesPicker){
            var n = new DynamicNode(referenceItem.InnerText);
            nodes.Add(n);
        }
    
        foreach (dynamic reference in nodes.Random(1))
        {

    @reference.referenceName

            @reference.referenceOneliner
            @reference.referenceText
    
        }
    }

    Thanks!

     

  • 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