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
    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 2342 posts 14869 karma points MVP 8x 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

Please Sign in or register to post replies

Write your reply to:

Draft