Copied to clipboard

Flag this post as spam?

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


  • Matthew Berner 1 post 71 karma points
    Mar 17, 2017 @ 14:13
    Matthew Berner
    0

    Error in Umbraco 7.43

    I am getting this error, it worked a few day ago and now it does not. I have two sites and tested on the second and get no error, same version. Same code is on both sites Error: 'Umbraco.Web.Models.DynamicPublishedContentList' does not contain a definition for 'Count' Code

          var root = CurrentPage.AncestorOrSelf(1);
    
    
    
    @foreach (var page in root.Children.Where("Visible"))
    {
        <li class="@(page.IsAncestorOrSelf(CurrentPage) ? "active " : null)">
            <a href="@page.Url">@page.Name</a>
            <ul class="nav nav-2">
                @foreach (var page3 in page.Children.Where("Visible"))
                {
                    <li class="@(page3.IsAncestorOrSelf(CurrentPage) ? "active " : null)">
                        <a href="@page3.Url">@page3.Name</a>
                       @if (@page3.Children.Where("Visible").Count() > 0)
                       {
                           <ul class="nav nav-3">
                            @foreach (var page4 in page3.Children.Where("Visible"))
                            {
                                <li class="@(page4.IsAncestorOrSelf(CurrentPage) ? "active " : null)">
                                    <a href="@page4.Url">@page4.Name</a>
                                </li>
                            }
                        </ul>
                       }
                    </li>
    
                }
            </ul></li>
    
            }
    

    Thanks Matt

  • Richard Eyres 98 posts 580 karma points
    Mar 20, 2017 @ 09:17
    Richard Eyres
    0

    I would try to avoid using the Dynamic model, and use the Model.Content instead. I made some VERY quick amends to the code, and totally untested.

    I would also recommend not counting the collection, but rather use the .Any() method, it makes better sense.

    It might also pay to check for children before trying to iterate over the collection as well.

    var root = Model.Content.AncestorOrSelf(1);
    
    @foreach (var page in root.Children.Where(x => x.IsVisible())) {
    <li class="@(page.Id == Model.Content.Id ? "active " : null)">
        <a href="@page.Url">@page.Name</a>
        <ul class="nav nav-2">
            @foreach (var page3 in page.Children.Where(x => x.IsVisible()))
            {
                <li class="@(page3.Id == Model.Content.Id ? "active " : null)">
                    <a href="@page3.Url">@page3.Name</a>
                    @if (page3.Children.Any())
                    {
                        <ul class="nav nav-3">
                            @foreach (var page4 in page3.Children.Where(x => x.IsVisible()))
                            {
                                <li class="@(page4.Id == Model.Content.Id ? "active " : null)">
                                    <a href="@page4.Url">@page4.Name</a>
                                </li>
                            }
                        </ul>
                    }
                </li>
            }
        </ul>
    </li>
    

    }

Please Sign in or register to post replies

Write your reply to:

Draft