Copied to clipboard

Flag this post as spam?

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


  • Jason Espin 368 posts 1335 karma points
    Apr 11, 2014 @ 12:06
    Jason Espin
    0

    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:

    @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.
    
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 11, 2014 @ 20:06
    Jeavon Leopold
    0

    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:

            @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.

    Jeavon

  • Jason Espin 368 posts 1335 karma points
    Apr 14, 2014 @ 10:24
    Jason Espin
    0

    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

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 14, 2014 @ 10:32
    Jeavon Leopold
    0

    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

  • Jason Espin 368 posts 1335 karma points
    Apr 14, 2014 @ 15:18
    Jason Espin
    0

    Hi Jeavon,

    I am using version 7.1.1 and it was a clean install.

    Jason

  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Apr 14, 2014 @ 16:00
    Peter Gregory
    0

    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 :)

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 14, 2014 @ 16:09
    Jeavon Leopold
    0

    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

  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Apr 14, 2014 @ 16:25
    Peter Gregory
    0

    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?

  • Jason Espin 368 posts 1335 karma points
    Apr 14, 2014 @ 18:17
    Jason Espin
    0

    Hi Peter,

    I can confirm that none of the picked items are in the Recycle Bin in the Media Section.

    Jason

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 16, 2014 @ 23:47
    Jeavon Leopold
    0

    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

  • Jason Espin 368 posts 1335 karma points
    Apr 22, 2014 @ 15:44
    Jason Espin
    101

    I ended up doing the following instead:

    @foreach (var bannerImage in bannerImagesCollection){
                var activeClass = imageCount < 1 ? "active" : string.Empty;
                <li data-target="#carousel" data-slide-to="@imageCount" class="@activeClass"></li> 
                imageCount++; 
            }
    
  • Ali Kazai 11 posts 82 karma points c-trib
    Mar 27, 2019 @ 13:49
    Ali Kazai
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft