Copied to clipboard

Flag this post as spam?

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


  • Craig100 1078 posts 2366 karma points c-trib
    Dec 09, 2017 @ 16:17
    Craig100
    0

    IsLast() IsNotWorking?

    Umb 7.7.6

    More curiosity than anything, but why doesn't IsLast() work in this scenario......

    var sitePages = Model.Site().Children();
    var mainNavList = sitePages.Where(x => x.IsVisible() && x.GetPropertyValue<bool>("includeInMainNav") == true);
    var footerNavList = sitePages.Where(x => x.IsVisible() && x.GetPropertyValue<bool>("includeInFooterNav") == true);
    
    <nav>
        <ul>
            @foreach(var page in footerNavList) {
                <li>
                    <a href="@page.Url" title="@page.Name" class="active">@page.Name</a>@page.IsLast("", " |&nbsp;")
                </li>
            }   
        </ul>                                                            
    </nav>
    

    I've got the effect I wanted in the end with the following but I just wondered why, as I am iterating over a collection of IPublishedContent so IsLast() should work shouldn't it?

    var sitePages = Model.Site().Children();
    var mainNavList = sitePages.Where(x => x.IsVisible() && x.GetPropertyValue<bool>("includeInMainNav") == true);
    var footerNavList = sitePages.Where(x => x.IsVisible() && x.GetPropertyValue<bool>("includeInFooterNav") == true);
    var lastItem = footerNavList.Last();
    
    <nav>
        <ul>
            @foreach(var page in footerNavList) {
                string divider = "";
                if(page != lastItem) {
                    divider = " |&nbsp;";
                }
                <li>
                    <a href="@page.Url" title="@page.Name" class="active">@page.Name</a>@Html.Raw(divider)
                </li>
            }   
        </ul>
    </nav>
    

    --Craig

  • Kevin Jump 1867 posts 11859 karma points MVP 4x c-trib
    Dec 09, 2017 @ 19:58
    Kevin Jump
    1

    HI,

    yeah i've tripped up on this a few times, but IsLast() doesn't actually behave the way you (or I have in the past) think it does.

    In the background IsLast goes of and gets all the Siblings of an Item and then checks to see if it's the last item in the list. So IsLast only returns true if the IPublishedContent item is the last one in the 'Content Set' which most of the time is the Siblings in the content tree.

    In my experience you are better off calling .ToList() at the end of the query and then using the List .Last() like you have

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies