Copied to clipboard

Flag this post as spam?

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


  • Amir Khan 1282 posts 2739 karma points
    Jul 11, 2014 @ 21:45
    Amir Khan
    0

    Related Links Razor Macro Umbraco 7

    Does anyone have any related links razor working with a macro for Umbraco 7?

    I unfortunately need to use the macro in the RTE and pass a parameter so can't use MVC, this just keeps throwing erros:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using Newtonsoft.Json.Linq


    @{
    var startNodeID = Parameter.ChooseButtonGroup;
    }

    @if (startNodeID != null)
    {
    @* Get the start node as a dynamic node *@
    <h1>@startNodeID</h1>
    var startNode = Library.NodeById(startNodeID);

    foreach(var relatedLink in startNode.GetPropertyValue<JArray>("relatedLinks")) {

    <h1>@(relatedLink.Value<string>("title"))</h1>

    }
    }


    The error I'm getting is "'char' does not contain a definition for 'Value'"

    -Amir

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jul 11, 2014 @ 22:09
    Jeavon Leopold
    0

    Hi Amir,

    Since v4.10 Partial View Macros have been the recommended macros for WebForms templates and Razor Macros are legacy. So you could use either of the snippets from here in a Partial View Macro

    Jeavon

  • Amir Khan 1282 posts 2739 karma points
    Jul 15, 2014 @ 16:20
    Amir Khan
    0

    Hi Jeavon, I can't seem to access any of the properties in the datatype via razor. Shouldn't just "@item.Caption" return the value in the caption field? The json looks like this:

     

    [{
    "caption":"Link1",
    "link":"http://google.com",
    "newWindow":false,
    "edit":false,
    "isInternal":false,
    "type":"external",
    "title":"Link1"
    },{
    "caption":"Link2",
    "link":"http://google.com",
    "newWindow":false,
    "edit":false,
    "isInternal":false,
    "type":"external",
    "title":"Link2"
    },{
    "caption":"Link3",
    "link":"http://google.com",
    "newWindow":false,
    "edit":false,
    "isInternal":false,
    "type":"external",
    "title":"Link3"
    }]
  • Amir Khan 1282 posts 2739 karma points
    Jul 15, 2014 @ 19:56
    Amir Khan
    2

    Incase anyone else has this issue:

    <ul>
    @foreach (dynamic item in JsonConvert.DeserializeObject(startNode.GetPropertyValue<string>("relatedLinks")))
    {

    var linkUrl = (bool)item.isInternal ? Library.NodeById(@item.link).Url : item.link;
    var linkTarget = (bool)item.newWindow ? "_blank" : null;
    <li><a href="@linkUrl" target="@linkTarget">@item.caption</a></li>

    }
    </ul>
  • Jules 269 posts 560 karma points
    Nov 27, 2014 @ 00:38
    Jules
    2

    Even simpler now in Umbraco 7.1.6

        @foreach (var link in CurrentPage.listOfLinks)
        {
            var linkTarget = (bool) link.newWindow ? "target=\"_blank\"" : "";
            <a href="@link.link" @Html.Raw(linkTarget)>@link.caption</a>
        }

    Jules

     

  • Christopher 4 posts 74 karma points
    Jun 30, 2016 @ 18:17
    Christopher
    0

    @Jules I'm using v.7.4.3 and your solution doesn't work, I get Amir's original error. For some reason umbraco is returning the "CurrentPage.listOfLinks" value as a json string, which causes the loop to go char by char.

  • Christopher 4 posts 74 karma points
    Jun 30, 2016 @ 18:58
    Christopher
    0

    OK this is really, really strange. The type of the property value changed suddenly from a string to the typed object... magically. I have no idea what caused this behaviour.

  • Chefdog 5 posts 26 karma points
    Mar 21, 2017 @ 18:56
    Chefdog
    0

    At the top of the view:

    var linksCollection = JsonConvert.DeserializeObject(Model.GetPropertyValue<string>("Links"));
    

    in the html. in my case a loop that writes out <li></li> :

    @if (linksCollection != null)
    {
        <aside class="widget widget-recent-posts animate-up">
            <h2 class="widget-title">Links &amp; Documents</h2>
            <ul>
                @foreach (dynamic item in (IEnumerable<dynamic>) linksCollection)
                {
                    var lnk = Json.Decode(item.ToString());
                    var linkTarget = (bool) lnk.newWindow ? "target=\"_blank\"" : "";
                    <li><a href="@lnk.link" @Html.Raw(linkTarget)>@lnk.caption</a></li>
                }
            </ul>
        </aside>
    
    }
    

    I am sure there could be some cleanup of the code, but it works....

Please Sign in or register to post replies

Write your reply to:

Draft