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];
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> } }
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?
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:
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
Hi Nicky ...
Maybe something like this - ?
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)
Greay, thx alot guys :)
Is there an updated version of this for V6 MVC Razor.
Tried this but complains about the [random] on the end
Craig
This is my solution:
@foreach(var t in Umbraco.Content(1205).Children().Random(1))
{
@t.testimonailDetaik
}is working on a reply...