Copied to clipboard

Flag this post as spam?

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


  • Dibs 202 posts 991 karma points
    Nov 14, 2016 @ 11:53
    Dibs
    0

    sub menu creation error

    Dear Umbraco Team

    I have the following development code for creating various sub menus, to meet certain requirements.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        var childPages = Model.Content.Children;
        var ancestors = Model.Content.Ancestors();
        var ancestorsOrSelf = Model.Content.AncestorOrSelf();
    }
    <ul>
        @foreach(var item in childPages)
        {
            <li><a href="@item.Url">@item.Name</a></li>
        }
    </ul>
    <ul>
        @foreach(var item in ancestors)
        {
            <li><a href="@item.Url">@item.Name</a></li>
        }
    </ul>
    <ul>
        @foreach(var item in ancestorsOrSelf)
        {
            <li><a href="@item.Url">@item.Name</a></li>
        }
    </ul>
    

    The code above give the following error for ancestorOrSelf variable:

    CS1579: foreach statement cannot operate on variables of type 'Umbraco.Core.Models.IPublishedContent' because 'Umbraco.Core.Models.IPublishedContent' does not contain a public definition for 'GetEnumerator'
    

    any reason why am i missing something simple ?

    Thanks Dibs

  • Eric Petersson 33 posts 117 karma points
    Nov 14, 2016 @ 11:59
    Eric Petersson
    0

    Looks like your variable declared is not of the list type. You also need to specify which node the iteration should be looking at.

  • Dibs 202 posts 991 karma points
    Nov 14, 2016 @ 12:05
    Dibs
    0

    Hi Eric

    ancestors variable works and is not declared as a list type

    thanks Dibs

  • Bijesh Tank 192 posts 420 karma points
    Nov 14, 2016 @ 12:11
    Bijesh Tank
    100

    Hi Dibs,

    I believe Model.Content.AncestorOrSelf() is not an IEnumerable, try using AncestorsOrSelf() instead (notice the difference between Ancestor and Ancestors).

    B.

  • Dibs 202 posts 991 karma points
    Nov 14, 2016 @ 12:18
    Dibs
    0

    Cheers Bijesh

    Thanks for the reply, i see my typo was to blame : (

    Thanks Dibs

  • Bijesh Tank 192 posts 420 karma points
    Nov 14, 2016 @ 12:25
    Bijesh Tank
    0

    No worries, glad to be of help :)

    B.

Please Sign in or register to post replies

Write your reply to:

Draft