Copied to clipboard

Flag this post as spam?

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


  • JamesW 18 posts 128 karma points
    Jan 12, 2018 @ 13:44
    JamesW
    0

    Get property values in Nested Content

    I'm trying to access propertis of some nested content but am having issues.

    I can loop through the content and display 'Name' but can't access any of the properties of each item.

    I have properties: title, intro, image

    and have tried:

    @foreach (var page in CurrentPage.Site().Children.Where("Visible"))
    {
    
        var items = page.GetPropertyValue<IEnumerable<IPublishedContent>>("sectionPanels");
    
        if (items != null)
        {
                    @foreach (var child in items)
                    {
                        <a href="">@child.Name</a></li>
                        <a href="">@child.GetPropertyValue("title")</a></li>
                    }
        }
    }
    

    As i said it will get the value for @child.Name but I get the error:

    Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ''Umbraco.Web.PublishedContentModels.ServiceProgramsSectionPanel' does not contain a definition for 'GetPropertyValue''

    When trying to access @child.GetPropertyValue("title")

    When debugging I see this for @child:

    enter image description here

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jan 12, 2018 @ 13:53
    Dave Woestenborghs
    0

    Hi James,

    This is because the a nested content item is not a real content item and therefore has no name.

    Dave

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jan 12, 2018 @ 13:54
    Nik
    102

    Hi James,

    As you've got Models builder enabled, you can create a typed model of your nested content.

    Make your foreach something like this:

    foreach(var rawChild in items)
    {
        var child = new ServiceProgramsSectionPanel(rawChild);
        <a href="">@child.Title</a></li>
    }
    

    I find doing this makes things easier.

    As Dave mentions, Nested content isn't a full content item. It is what's called a disconnected content type (I think). That doesn't explain why the GetPropertyValue method isn't working, it works for me normally. But if you use a Typed Model, it works around it I find.

    Nik

Please Sign in or register to post replies

Write your reply to:

Draft