Copied to clipboard

Flag this post as spam?

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


  • Craig O'Mahony 364 posts 918 karma points
    Jun 17, 2015 @ 12:43
    Craig O'Mahony
    0

    Looping though Related Items datatype

    Hi all,

    I've got a folder (empty doctype) which contains some nodes. The only property on these nodes is a Related Links datatype.

    So what I'm trying to do is to loop through the nodes and loop through the Related Links for each node but all of the code that I have tried is falling over. Could someone point out my stupidity please?

    Screen shot of set up: enter image description here

    And the code that I think should work but doesn't!

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using System.Linq;
    @using umbraco.MacroEngines;
    
    @{var node = Umbraco.TypedContent(1247);}
    
    @{
        foreach (var child in node.Children)
        {
                <p>@child.Name.ToUpper()</p>
                <ul class="list-unstyled footerList">
                    @if (child.HasValue("footerLinks"))
                    {
                        foreach (var link in child.footerLinks)
                        {
                            //Display the links
                        }
    
    
                    }
                </ul>
        }
    }
    

    On the line foreach (var link in child.footerLinks) I get the error Umbraco.Core.Models.iPublishedcontent does not contain a definition for footerlinks but elsewhere on the forums this appears to be the way that it's been done.

    Any help would be greatly received.

    Thanks, Craig

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Jun 17, 2015 @ 13:13
    Dennis Aaen
    0

    Hi Craig,

    You are getting the error Umbraco.Core.Models.iPublishedcontent does not contain a definition for footer links error because you are mixing the strongly typed Razor and the dynamic Razor.

    Try to do something like this would this work for you.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using System.Linq;
    @using umbraco.MacroEngines;
    
    @{var node = Umbraco.TypedContent(1247);}
    
    @{
        foreach (var child in node.Children.Where(x => x.IsVisible())
        {
                <p>@child.Name.ToUpper()</p>
                <ul class="list-unstyled footerList">
                    @if (child.HasValue("footerLinks"))
                    {
                        foreach (var link in child.Children("footerLinks").Where(x => x.IsVisible())
                        {
                            //Display the links
                        }
    
    
                    }
                </ul>
        }
    }
    

    Hope this helps,

    /Dennis

  • Craig O'Mahony 364 posts 918 karma points
    Jun 17, 2015 @ 13:35
    Craig O'Mahony
    0

    Hi Dennis,

    Thanks for that but copying the code exactly I'm getting a Umbraco.Core.Models.iPublishedContent does not contain a definition for Children on

    foreach (var link in child.Children("footerLinks").Where(x => x.IsVisible())

    To confirm the 'child' node doesn't actually have any children but it does contain the Related Items data that I'm struggling to get to.

    thanks again....

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Jun 17, 2015 @ 17:06
    Dennis Aaen
    100

    Hi Craig,

    Okay now I know what you are trying to do.

    Try to use the following code snippet

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using System.Linq;
    @using umbraco.MacroEngines;
    
    @{var node = Umbraco.TypedContent(1247);}
    
    @{
        foreach (var child in node.Children)
        {
                <p>@child.Name.ToUpper()</p>
                <ul class="list-unstyled footerList">
                   @if (child.HasValue("footerLinks") && child.GetPropertyValue<string>("footerLinks").Length > 2){
                        <ul>
                            @foreach (var item in child.GetPropertyValue<JArray>("footerLinks"))
                            {
                                var linkUrl = (item.Value<bool>("isInternal")) ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.Value<string>("link");
                                var linkTarget = item.Value<bool>("newWindow") ? "_blank" : null;
                                <li><a href="@linkUrl" target="@linkTarget">@(item.Value<string>("caption"))</a></li>
                            }
                        </ul>
                            }
                </ul>
        }
    }
    

    You can find the documentation for the related links here: https://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/built-in-property-editors-v7/Related-Links

    Hope this helps,

    /Dennis

  • Craig O'Mahony 364 posts 918 karma points
    Jun 18, 2015 @ 13:02
    Craig O'Mahony
    0

    Bang on! Cheers matey

Please Sign in or register to post replies

Write your reply to:

Draft