Copied to clipboard

Flag this post as spam?

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


  • Craig100 1136 posts 2523 karma points c-trib
    Mar 27, 2015 @ 18:31
    Craig100
    0

    Nested IsFirst()

    Umb 7.2.2

    The code below should produce:-

    Hello0
    Hello1
    Hello2

    But it always produces:-

    Hello0
    Hello2

    @foreach (var thisSku in skuList) {
        if (thisSku.HasValue("productImages")) {
            var productImagePicker = thisSku.GetPropertyValue<IEnumerable<IPublishedContent>>("productImages");
            if(thisSku.IsFirst()){
                <p>hello0</p>
                foreach(var productImage in productImagePicker) { 
                    //<p>hello3</p>
                    if(productImage.IsFirst()){
                        <p>hello1</p>                   
                    }
                }
            } else {
                foreach(var productImage in productImagePicker) {
                    if(productImage.IsFirst()){
                        <p>hello2</p>                   
                    }
                }
            }                                                                        
        }                                                                               
    }

    It is totally doing my head in as there are the same images in each image picker. If I uncomment hello3 I get many hello3s, but can't show just the first one.

    Is there a bug with nested .IsFirst()?

    Any advice appreciated.

    Craig

  • Craig100 1136 posts 2523 karma points c-trib
    Mar 28, 2015 @ 13:44
    Craig100
    0

    Well in the end I had to resort to a counter. I feel dirty....

    if(thisSku.IsFirst()){
        int counter = 0;
        foreach(var productImage in productImagePicker) {
            if(counter == 0) {
                <img src="@productImage.GetResponsiveCropUrl("ProductShot")" data-sku="@thisSku.GetProperty(skuArea).DataValue.ToString()" alt="@productImage.Name" class="productShot">
            }
            counter++;
        }
    } 

    HTH someone else,

    Craig

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Mar 28, 2015 @ 13:59
    Jeavon Leopold
    101

    Hi Craig,

    Give this a try

    foreach(var productImage in productImagePicker.ToContentSet()) {
    

    Jeavon

  • Craig100 1136 posts 2523 karma points c-trib
    Mar 28, 2015 @ 14:18
    Craig100
    0

    Thanks Jeavon, I'm clean again :)

    Googling on "ToContentSet" brings up this link: https://gist.github.com/zpqrtbnk/6582377, with a handy reason for the issue in the comments:-

    "because of some issues in IPublishedContent, IsFirst, IsOdd, Index... all gave random results as soon as contents was not the origin children collection. this has been fixed but requires the usage of the .ToContentSet() method"

    Craig

Please Sign in or register to post replies

Write your reply to:

Draft