I'm currently in the process of refining my code to try and tidy up any unneccesary variables and instead make sure of some of the DynamicNode Razor methods but I'm worried that I may be using them incorrectly.
In the example below I have a media selector that allows the user to select images that are then output as part of a carousel on my homepage. To ensure that the Carousel works correctly, the first item that is output must have an active class appended to both the image and the indicator which indicates the current slide that the user is on. To do this, I have tried to make use of the IsFirst method:
@if (Model.Content.HasValue("bannerImages")){
var bannerImagesList = Model.Content.GetPropertyValue<string>("bannerImages").Split(new string[]{","}, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
var bannerImagesCollection = Umbraco.TypedMedia(bannerImagesList).Where(x => x != null);
var imageCount = 0;
<div id="carousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
@foreach (var bannerImage in bannerImagesCollection){
var activeClass = bannerImage.IsFirst("active",string.Empty);
<li data-target="#carousel" data-slide-to="@imageCount" class="@activeClass"></li>
}
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner">
@foreach (var bannerImage in bannerImagesCollection){
var cssClass = imageCount < 1 ? "item active" : "item";
<div class="@cssClass">
<img src="@bannerImage.Url" alt="@bannerImage.Id" />
</div>
imageCount++;
}
</div>
</div>
}
Ultimately my aim is to make the imageCount variable redundant and instead make usre of the .IsFirst method to conditionally append the classes to the correct elements. My question is, would this method be out of context here as I am receiving the following error in the YSD:
Line 11: var activeClass = bannerImage.IsFirst("active",string.Empty);
Exception Details: System.IndexOutOfRangeException: Could not find content in the content set.
I have given your code and try and it works perfectly for me without modification which is puzzling...
But the error is indicating that a check on 'bannerImage' might solve it, although why it doesn't contain content is odd.
Anyhow, how about trying:
@foreach (var bannerImage in bannerImagesCollection)
{
if (bannerImage != null)
{
var activeClass = bannerImage.IsFirst("active", string.Empty);
<li data-target="#carousel" data-slide-to="@imageCount" class="@activeClass"></li>
}
}
It might able be worth flushing your Examine indexes in case something has become corrupted. To do this, stop your application pool, then delete the folder /App_Data/Temp/ExamineIndexes, then start your app pool.
I have tried both adding a check as to whether the value is null and have also tried flushing the examine indexes. Unfortunately, I am still being presented with the same error.
The reason why IsFirst is not working for you is because its implementation is not quite what it sounds like. IsFirst actually returns whether or not the item in the list is first in its actual context. EG.
Say I had a list of items.
Item 1
Item 2
Item 3
Item 4
And then somewhere else pick to display on the page. Lets say in the following order.
Item 2
Item 4
Item 3
The IsFirst does not refer to the collection you are iterating over but the original list that it came from. Confusing right? :)
The same goes for IsLast
So with our picked list we would get nothing firing for IsFirst, because none of those items is first in its original context, and we would get IsLast firing on the second item as Item 4 is last in its original context.
To get a proper IsFirst IsLast implementation in your scenario it would be better to work with it as an Array and use the indexes.
I didn't think this was the case in v4/v6 where items did become aware of the collection there were in for the "Is Helpers" although this was not the case when MVC was first introduced. There is a lengthy discussion between myself, Shannon and Doug about this here.
I will have a check in v7 and see if it's changed back but that would be a big breaking change!
I'm afraid I've run out of ideas on this one, is it possible that you could send me a copy of your files and database (what type of DB are you using) so I can see what's going on?
Thanks for that, for some reason i had tested the .IsFirst() and it worked for me then when i deployed and added some content it stopped working after debugging i noticed that the foreach loop is working but everything is hidden as there was no "active" item.
You would think the IsFirst() helper would do its job as stated lol.
Using IsFirst on a collection in Umbraco 7
Hi all,
I'm currently in the process of refining my code to try and tidy up any unneccesary variables and instead make sure of some of the DynamicNode Razor methods but I'm worried that I may be using them incorrectly.
In the example below I have a media selector that allows the user to select images that are then output as part of a carousel on my homepage. To ensure that the Carousel works correctly, the first item that is output must have an active class appended to both the image and the indicator which indicates the current slide that the user is on. To do this, I have tried to make use of the IsFirst method:
Ultimately my aim is to make the imageCount variable redundant and instead make usre of the .IsFirst method to conditionally append the classes to the correct elements. My question is, would this method be out of context here as I am receiving the following error in the YSD:
Hi Jason,
I have given your code and try and it works perfectly for me without modification which is puzzling...
But the error is indicating that a check on 'bannerImage' might solve it, although why it doesn't contain content is odd.
Anyhow, how about trying:
It might able be worth flushing your Examine indexes in case something has become corrupted. To do this, stop your application pool, then delete the folder /App_Data/Temp/ExamineIndexes, then start your app pool.
Jeavon
Hi Jeavon,
I have tried both adding a check as to whether the value is null and have also tried flushing the examine indexes. Unfortunately, I am still being presented with the same error.
/Jason
Hi Jason,
Could you confirm your Umbraco version and also if it was a clean install at that version or if it was upgraded?
Thanks,
Jeavon
Hi Jeavon,
I am using version 7.1.1 and it was a clean install.
Jason
Hi Jason
The reason why IsFirst is not working for you is because its implementation is not quite what it sounds like. IsFirst actually returns whether or not the item in the list is first in its actual context. EG.
Say I had a list of items.
Item 1 Item 2 Item 3 Item 4
And then somewhere else pick to display on the page. Lets say in the following order.
Item 2 Item 4 Item 3
The IsFirst does not refer to the collection you are iterating over but the original list that it came from. Confusing right? :)
The same goes for IsLast
So with our picked list we would get nothing firing for IsFirst, because none of those items is first in its original context, and we would get IsLast firing on the second item as Item 4 is last in its original context.
To get a proper IsFirst IsLast implementation in your scenario it would be better to work with it as an Array and use the indexes.
Hope that clarifies it for you :)
Hi Peter,
I didn't think this was the case in v4/v6 where items did become aware of the collection there were in for the "Is Helpers" although this was not the case when MVC was first introduced. There is a lengthy discussion between myself, Shannon and Doug about this here.
I will have a check in v7 and see if it's changed back but that would be a big breaking change!
Jeavon
Hi Jeavon
You might be right?! I have to double check but I am pretty sure I had issues with it in v6.1.6??
However i just reread the post. I didnt read the last line :| (facepalm)
Jason: Have you checked to make sure that none of the picked items is in the Recycle Bin in the Media Seciton?
Hi Peter,
I can confirm that none of the picked items are in the Recycle Bin in the Media Section.
Jason
Hi Jason,
I'm afraid I've run out of ideas on this one, is it possible that you could send me a copy of your files and database (what type of DB are you using) so I can see what's going on?
You can DM me on Twitter.
Jeavon
I ended up doing the following instead:
Thanks for that, for some reason i had tested the .IsFirst() and it worked for me then when i deployed and added some content it stopped working after debugging i noticed that the foreach loop is working but everything is hidden as there was no "active" item.
You would think the IsFirst() helper would do its job as stated lol.
anyway your technique worked great. thank you
is working on a reply...