Does anyone know a better way to do the following?
I am trying to use 3 different random testimonials in a slider but not sure how to get three different random items and render them in the right
.
@{
var FirstTestimonial = Model.Content.Site().FirstChild("Testimonial").Children().RandomOrder().FirstOrDefault();
var SecondTestimonial = Model.Content.Site().FirstChild("Testimonial").Children().RandomOrder().FirstOrDefault();
var ThirdTestimonial = Model.Content.Site().FirstChild("Testimonial").Children().RandomOrder().FirstOrDefault();
@renderTestimonial(FirstTestimonial)
@renderTestimonial(SecondTestimonial)
@renderTestimonial(ThirdTestimonial)
}
This works but sometimes I get 2 items the same. I would like each testimonial to be different but random.
As per your code, it's true that you get the same items.
First, take a list of the random item up to 3 then through looping you can display that 3 random items you don't need to take three variable like FirstTestimonial, SecondTestimonial and ThirdTestimonial Just one variable is fine for that.
Thanks for the replys but unfortunately the slider is control by script and I need to place the results in the controlling divs. Without the div class "active item" and the following div classes "item" this does not work.
In your code, the first item is active, and the rest are not.
Using this logic, you could refactor your code, using a single variable (instead of 3) and still produce your expected output by using a counter to determine which items in your testimonials should have the "active" class.
For example, like so:
@{
var testimonials = Model.Content.Site().FirstChild("Testimonial").Children().RandomOrder().Take(3);
var counter = 1;
<div class="carousel-inner">
@foreach (var testimonial in testimonials)
{
// If the counter is equal to 1, then set the item class to active
var isActive = counter == 1 ? "active item" : "";
<div class="@isActive item">
@renderTestimonial(testimonial)
</div>
// Increment the counter, so you can't have two active items at a time
counter++;
}
</div>
}
Personally, I find this easier to read than having a counter that's increment all the time. If you need the counter for something else however then I'd go down that route :-)
(It comes down to personal preference here I think, just thought it worth sharing the alternative)
Using random items in a slider
Does anyone know a better way to do the following?
I am trying to use 3 different random testimonials in a slider but not sure how to get three different random items and render them in the right
@{ var FirstTestimonial = Model.Content.Site().FirstChild("Testimonial").Children().RandomOrder().FirstOrDefault(); var SecondTestimonial = Model.Content.Site().FirstChild("Testimonial").Children().RandomOrder().FirstOrDefault(); var ThirdTestimonial = Model.Content.Site().FirstChild("Testimonial").Children().RandomOrder().FirstOrDefault();
}
This works but sometimes I get 2 items the same. I would like each testimonial to be different but random.
Hello Mark Watson,
As per your code, it's true that you get the same items.
First, take a list of the random item up to 3 then through looping you can display that 3 random items you don't need to take three variable like FirstTestimonial, SecondTestimonial and ThirdTestimonial Just one variable is fine for that.
Regards, Ronak Panchal
As Ronak pointed out, you probably don't need to have three separate variables for this.
The following code should work:
Thanks for the replys but unfortunately the slider is control by script and I need to place the results in the controlling divs. Without the div class "active item" and the following div classes "item" this does not work.
In your code, the first item is active, and the rest are not.
Using this logic, you could refactor your code, using a single variable (instead of 3) and still produce your expected output by using a counter to determine which items in your testimonials should have the "active" class.
For example, like so:
Rhy's solution is nice, but instead of using a counter you could use a Boolean to track if this is the first iteration when using a foreach loop.
eg:
Personally, I find this easier to read than having a counter that's increment all the time. If you need the counter for something else however then I'd go down that route :-)
(It comes down to personal preference here I think, just thought it worth sharing the alternative)
Thanks Rhys and Nik
Both worked perfectly.
is working on a reply...