Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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.
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
Hi Dennis
The problem is the ArchetypeModel does not contain the methods Take() and Random(), so these cannot be used in this context.
/Signe
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="" /> }
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).
can you try Take(3).ToList().Random().ToList()
And if that doesn't work you can always google on c# list random. You will find plenty of examples.
Dave
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
@signe, If you look carefully. It's not .Random(), it is
.RandomOrder()
Oh, my mistake! Yeah it works now - thanks a lot :-)
:) @signe If your problem is solved, remember to mark as "solved".
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
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:
Any ideas on how to do that?
Even better if I can randomize and only pullout 3 sponsors.
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
Hope this helps,
/Dennis
Hi Dennis
The problem is the ArchetypeModel does not contain the methods Take() and Random(), so these cannot be used in this context.
/Signe
Have you try using Model.Content instead of CurrentPage
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).
can you try Take(3).ToList().Random().ToList()
And if that doesn't work you can always google on c# list random. You will find plenty of examples.
Dave
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
@signe, If you look carefully. It's not .Random(), it is
Oh, my mistake! Yeah it works now - thanks a lot :-)
:) @signe If your problem is solved, remember to mark as "solved".
is working on a reply...