Copied to clipboard

Flag this post as spam?

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


  • nickornotto 403 posts 907 karma points
    May 15, 2017 @ 14:30
    nickornotto
    0

    Error on looping through a RelatedLinks property of dynamic Node in Razor

    I have a Razor partial which displays my site navigation:

        @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
        @{
            var home = CurrentPage.Site();
            umbraco.NodeFactory.Node navigationSettingsNode = MySite.Umbraco.NavigationSettings;
            dynamic navigationSettings = new umbraco.MacroEngines.DynamicNode(navigationSettingsNode.Id);
            var settings = home.Children.Where("DocumentTypeAlias == \"Settings\"").First();
        }
    
    
    @if (navigationSettings.HasValue("topNavigation"))
    {
            <ul>
                dynamic topNavigation = navigationSettings.topNavigation;
                var topNavigation2 = settings.topNavigation;
                <span>@topNavigation</span>
                <span>@topNavigation2</span>
    
                foreach(dynamic item in topNavigation)
                {
                <li>
                    <a href="@item.link" title="@item.title">@item.caption</a>
                </li>
                }
            </ul>
    }
    

    Initially I was looping through topNavigation2 items which worked fine and with no problem.

    Now I'm looping through topNavigation items and it throws an error:

    Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'char' does not contain a definition for 'link'
    

    I don't want to use var settings anymore, I want to use only dynamic navigationSettings variable. In order to get the right node of navigationSettings I need to some operation and I don't fancy to paste the same code in every view I want to use it so I want it to be accessible from dll and available to use anywhere.

    Also the navigationSettings node in my Umbraco is outside of main content tree so is not a child of Home.

    Why isn't it working? Both

                dynamic topNavigation = navigationSettings.topNavigation;
                var topNavigation2 = settings.topNavigation;
    

    produce the same json result and both are dynamic objects.

    How to make it work correctly?

    I'm using MVC 5.2.3

  • Alex Skrypnyk 6182 posts 24283 karma points MVP 8x admin c-trib
    May 15, 2017 @ 14:38
    Alex Skrypnyk
    0

    Hi Eva

    'topNavigation' is a RelatedLinks property?

    Why do you want to use dynamics only?

    What version of Umbraco are you using?

    Thanks

    Alex

  • nickornotto 403 posts 907 karma points
    May 15, 2017 @ 15:43
    nickornotto
    0
    1. Yes
    2. I don't want to use dynamics only, I just made topNavigation dynamic the same as topNavigation2 was
    3. 7.5.12
  • Alex Skrypnyk 6182 posts 24283 karma points MVP 8x admin c-trib
    May 15, 2017 @ 15:53
    Alex Skrypnyk
    101

    Hi Eva

    I rewrote your code little bit, try to use this code for rendering 'topNavigation', 1111 is navigationSettingsNode.Id;

    @using Newtonsoft.Json.Linq
    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        var navigationSettings = Umbraco.TypedContent(1111);
    }
    
    
    @if (navigationSettings.HasValue("topNavigation"))
    {
        <ul>
            @foreach (var item in navigationSettings.GetPropertyValue<JArray>("topNavigation"))
            {
                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>
    }
    

    /Alex

  • Alex Skrypnyk 6182 posts 24283 karma points MVP 8x admin c-trib
    May 16, 2017 @ 08:52
    Alex Skrypnyk
    0

    Hi Eva

    Did you solve this issue? Can we help you?

    Thanks,

    Alex

  • nickornotto 403 posts 907 karma points
    May 18, 2017 @ 10:12
    nickornotto
    0

    Hi,Yes! it works. Thank you for quick help!

  • Alex Skrypnyk 6182 posts 24283 karma points MVP 8x admin c-trib
    May 18, 2017 @ 12:14
    Alex Skrypnyk
    0

    Hi Eva

    Really glad to help :) Have a nice day!

    Alex

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

    Continue discussion

Please Sign in or register to post replies