Copied to clipboard

Flag this post as spam?

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


  • Nicky Christensen 76 posts 166 karma points
    Oct 16, 2012 @ 23:36
    Nicky Christensen
    0

    display random node?

    Hey guys... 

    I have a macro where i want to display a random node from umbraco, but it doesnt seem like I can use the Random() method? Any ideas to how I do this?

    @RAZOR *@
    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{
      foreach (var item in @Model.NodeById(1399).Children.Where("NodeTypeAlias == \"FitnessAd\"").Random(){
      
       <div class="ad">
         @Html.Raw(@item.fitnessAd)
       </div>
                                                                                            
      
    }
  • Tom Madden 252 posts 454 karma points MVP 4x c-trib
    Oct 17, 2012 @ 00:22
    Tom Madden
    0

    Nicky,

    AFAIK, Random() will return a single node, so you can't iterate through it using a foreach, instead, change your foreach line to the following:

    varitem =@Model.NodeById(1399).Children.Where("NodeTypeAlias == \"FitnessAd\"").Random());

    Remove the curly bracket from the end of that line as well as the closing curly from the foreach statement and you should be good to go

    HTH

    Tom

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Oct 17, 2012 @ 00:26
    Rusty Swayne
    1

    Hi Nicky ...

    Maybe something like this - ? 

    @* RAZOR *@
    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{
    dynamic ad = new DynamicNode(1399).ChildrenAsList.OrderBy(n => Guid.NewGuid()).FirstOrDefault();

    if(fitnessAd != null)
    {
    <div class="ad">
    @Html.Raw(@ad.fitnessAd)
    </div>
    }
  • Rasmus Fjord 675 posts 1566 karma points c-trib
    Oct 17, 2012 @ 08:36
    Rasmus Fjord
    1

    I think ill try something like this. 

    Here you dont have to loop all items through just get a count and pick an item from your list based on the random class.(i know its not the best random and blah blah blah but here it gets the job done)

     

    @* RAZOR *@
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
    int max = Model.NodeById(1399).Children.Where("NodeTypeAlias == \"FitnessAd\"").Count();    
    Random r = new Random();
    int randomNumber = r.Next(0,max);
     
    var item = Model.NodeById(1399).Children.Where("NodeTypeAlias == \"FitnessAd\"")[randomNumber];  
      
        <div class="ad">
            @Html.Raw(@item.fitnessAd)
        </div>
                                                                                            
       
    }
  • Nicky Christensen 76 posts 166 karma points
    Oct 17, 2012 @ 10:12
    Nicky Christensen
    1

    Greay, thx alot guys :) 

  • Craig100 1136 posts 2523 karma points c-trib
    Apr 15, 2014 @ 05:34
    Craig100
    0

    Is there an updated version of this for V6 MVC Razor.

    Tried this but complains about the [random] on the end

    @{
    if(Model.Content.AncestorOrSelf(1).Descendants("FeedbackItem").Any()) {
    int max = Model.Content.AncestorOrSelf(1).Descendants("FeedbackItem").Count(); 
    Random r = new Random();
    int randomNumber = r.Next(1,max);
    var item =  Model.Content.AncestorOrSelf(1).Descendants("FeedbackItem")[randomNumber];
    <h2>@item.feedbackBody</h2>
    }
    }

    Craig

  • Daniel Williams 16 posts 38 karma points
    Sep 24, 2014 @ 11:12
    Daniel Williams
    0

    This is my solution:

    var QuestionCollection = Universe.Descendants("Question").ToList();

    @if( QuestionCollection.Any() ) {
      Int32 questionCount = QuestionCollection.Count();
      Random random = new Random();
      Int32 randomNumber = random.Next( 0, questionCount );
      var Question = QuestionCollection[randomNumber];
    }

    @Question.Name
  • Mark Downie 8 posts 28 karma points
    Dec 08, 2016 @ 21:41
    Mark Downie
    0

    @foreach(var t in Umbraco.Content(1205).Children().Random(1))
    {

    @t.testimonailDetaik

    }

Please Sign in or register to post replies

Write your reply to:

Draft