Copied to clipboard

Flag this post as spam?

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


  • Matthew 138 posts 201 karma points
    Apr 15, 2014 @ 03:02
    Matthew
    0

    list children of a once removed node on homepage

    Sorry for the double posting, getting desperate.  Trying to list just the children of a specific node (Sectors, below), so content is like this:

    Home
    - Sectors
    - - Sector1
    - - Sector2
    - - Sector3
    - Other section
    - - stuff
    - - more stuff
    - Etc
    - - etc etc

    and in a browser it looks like this:

    Homepage content
    Sector1 | Sector2 | Sector3

    I don't understand why I can query successfully for the node's title (Sectors):

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    var
    root = Model.Content.AncestorOrSelf(1);
    var sectionTitle = root.Descendants().Where(x => x.GetPropertyValue("title") == "Sectors");

    but can't seem to get my claws on its descendants:

    @foreach (var node in sectionTitle.Descendants())
    {
        <p>@node.Name</p>}p>}

    When I try to use that, it's not happy about putting 'Descendants' in that spot:

    'System.Collections.Generic.IEnumerable<Umbraco.Core.Models.IPublishedContent>' does not contain a definition for 'Descendants' and the best extension method overload 'Umbraco.Core.Models.ContentExtensions.Descendants(Umbraco.Core.Models.IContent)' has some invalid arguments

    although Descendants() works fine elsewhere, up above and in other versions/attempts.

    Help?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Apr 15, 2014 @ 12:08
    Jeroen Breuer
    1

    Hello,

    Your sectionTitle is an IEnumerable instead of a single IPublishedContent object. So try this:

    var sectionTitle = root.Descendants().Where(x => x.GetPropertyValue("title") == "Sectors").First();

    Than it should be an IPublishedContent and Descendants should work.

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Apr 17, 2014 @ 12:13
    Jeroen Breuer
    0

    Hello,

    Did the above code worked for you?

    Jeroen

  • Matthew 138 posts 201 karma points
    Apr 18, 2014 @ 00:13
    Matthew
    0

    Hi Jeron,

    Sorry for the slow reply.  Kevin suggested in my other post in Razor, 'Retrieve children of a lower branch... ', to simplify my concept, which seemed like a good idea so I've been back at work and forgot to look here.

    That said, until you mentioned it, I hadn't registered what IEnumberable and iPublishedContent really meant and now I've wandered off to look harder at those.  It looks like iEnumerable is a C# thing for collections (expl), chiefly it enables the foreach loop, and iPublishedContent is an Umbraco thing for a (generally singel) content item ('Querying and Traversal' and 'Razor Feature Walkthrough 1').  I guess I'm not really getting what iPublishedContent is though.

    I see 'Descendants()' on the v.6 cheat sheet is of the Collections group which would align with the C# info, and there's an item '.IsDescendant()' (singular) that references IPublishedContent.  If I'd used IsDescendat() instead of Descendants(), would it have worked the same as your suggestion to add '.First()'?  It can't be that simple... can it?

    Thank you much for the guidance.  Love to learn.

Please Sign in or register to post replies

Write your reply to:

Draft