Copied to clipboard

Flag this post as spam?

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


  • Tom 161 posts 322 karma points
    Mar 17, 2015 @ 14:36
    Tom
    0

    How to get Count

    I recently downloaded Umbraco 7plus the starter kit.
    I then focused my attention on the MainNavigation.cshtml page.
    Since I wanted intellisense in my razor view, I changed out this code
    @{ var home = CurrentPage.Site(); }
    @if (home.Children.Any())

    to 

    @if (Model.Content.Children.Any())

    But when the code gets to this line @childPages(childPage.Children) and calls the helper method
    @helper childPages(dynamic pages), I'm getting an error, pages.Any() is null.

     

    Question:  How do I convert the pages.Any() to get the collection count.  Note: Pages ResultsView shows three items (The Starter Kit, Basics, MasterClasses").

     

    Thanks

    here is the code

     

    @if (Model.Content.Children.Any())

    {

        @* Get the first page in the children *@

        var naviLevel = Model.Content.Children.First().Level;  

     

        @* Add in level for a CSS hook *@

        <ul class="level-@naviLevel">            

            @* For each child page under the home node *@

            @foreach (var childPage in Model.Content.Children)

            {   

                if (childPage.Children.Any())

                {                    

                    <li class="has-child @(childPage.IsAncestorOrSelf(Model.Content) ? "selected" : null)">

    @if(childPage.DocumentTypeAlias == "LandingPage") 

    {

                        <span>@childPage.Name</span>

    @childPages(childPage.Children)

    } else {

    <a href="@childPage.Url">@childPage.Name</a>

    }

                    </li>

                } 

    else

                {

                    <li class="@(childPage.IsAncestorOrSelf(Model.Content) ? "selected" : null)">

                        <a href="@childPage.Url">@childPage.Name</a>

                    </li>

                }            

            }

        </ul>

    }

     

    @helper childPages(dynamic pages)

    {

        @* Ensure that we have a collection of pages *@

        if (pages.Any())

        {

            @* Get the first page in pages and get the level *@

     

    Code dies in the helper method line     if (pages.Any())

     

    Thanks Tom

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 17, 2015 @ 15:38
    Jan Skovgaard
    0

    Hi Tom

    Have you tried using Count? Or Count()?

    Like this:

    @if (Model.Content.Children.Count > 0)
    

    /Jan

  • Tom 161 posts 322 karma points
    Mar 17, 2015 @ 16:00
    Tom
    0

    Jan:


    Thanks for replying.

    Count does not work on  the pages object (childPage.Children)  being passed in as Dynamic.

    Yes, when I query Model.Content.Children.Count it returns 5, which is wrong.  Since the parent object is Learn , there are only 3 children (The Starter Kit, Basics, MasterClasses").

     

    When I break into the code, The non public member (Results View) shows 3.  I just don't know the systax to write to iterate through.

    Note:  the object "pages" is coming in as System.Linq.OrderedEnumerable<Umbraco.Core.Models.IPublishedContent,int>}

     

    Do you know I could cast pages or convert to so I can iterate through?

    Tom

     


     

  • Patrick Robin 34 posts 104 karma points
    Mar 17, 2015 @ 16:04
    Patrick Robin
    0

    rather than if (pages.Any()) can you not use if (pages != null && pages.Any())? I'm assuming that the problem is due to pages being null which causes .Any() to throw an exception which woiuld be the same even if you change it to use Count().

  • Tom 161 posts 322 karma points
    Mar 17, 2015 @ 16:15
    Tom
    0

    That didn't work either and I know pages.Any() is valid as when I break into the code, I see 3 values in the ResultsView.

    I think if you could help me convert my pages object (System.Linq.OrderedEnumerable<Umbraco.Core.Models.IPublishedContent,int>} to something??? that's of the right type, then perhpas I could iterate through.

     

    Thanks for helping.

     

    Tom

  • Tom 161 posts 322 karma points
    Mar 17, 2015 @ 17:25
    Tom
    0

    Can anyone help?

  • Richard Eyres 98 posts 580 karma points
    Mar 18, 2015 @ 18:20
    Richard Eyres
    0

    Personally, i hate dymanics, so i would try to accept an iEnumberable. You may need to pass a List. Give that a quick try.

  • Tom 161 posts 322 karma points
    Mar 18, 2015 @ 19:44
    Tom
    0

    Richard:

    I'm curious.  What would you replace dynamics with?

    And can you help explain your answer?  How do I convert this code @childPages(childPage.Children) to a list?

     

    Thanks

     

    Tom

  • Tom 161 posts 322 karma points
    Mar 24, 2015 @ 18:53
    Tom
    0

    OK I got it to work by doing changing my helper method

    from this

    @helper childPages(dynamic pages)

    to this

    @helper childPages(IEnumerable<IPublishedContent> pages)

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft