Copied to clipboard

Flag this post as spam?

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


  • Guy Malfait 23 posts 74 karma points
    Jan 22, 2016 @ 16:29
    Guy Malfait
    0

    Retrieve Related Links from child

    Hello ...

    I have a document A, which has a child B that contains a Related Links property called "Events".

    I'm trying to display the Events-links in A, using a partial view macro.

    Here's what I have ..

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Newtonsoft.Json.Linq
    
    @{
        var s = CurrentPage.Children.First();
        if (s.HasValue("Events") && s.Events.ToString().Length > 2)
        {
            <ul>
                @foreach (var item in s.Events)
                {
                    <li>
                        @(item)
                    </li>
                }
            </ul>
        }
    }
    

    This produces the following output

    [
    {
    "
    c
    a
    p
    t
    i
    o
    n
    "
    :
    "
    T
    o
    u
    r
    D
    o
    w
    n
    U
    n
    d
    e
    r
    

    So it is giving me all the characters separately, instead of returning links!

    (I have tried a different approach, with GetPropertyValue<JArray>("Events"), but that gives me an error, the call returns a null reference. It doesn't give an error for GetPropertyValue<string>("Events"), but that returns only the characters, as above, so not useful).

    Any ideas?

    Thanks

    Guy

  • Guy Malfait 23 posts 74 karma points
    Jan 22, 2016 @ 17:29
    Guy Malfait
    0

    I should have posted earlier, because I found a way around it straight afterwards.

    If I change this line

    @foreach (var item in s.Events)
    

    for this one

    @foreach (var item in JArray.Parse(s.Events))
    

    ... it works!

    Any idea why the JSON had to be explicity parsed? It came out like this (pre-parsing):

    [ { "caption": "Tour Down Under", "link": "http://", "newWindow": false, "edit": false, "isInternal": false, "type": "external", "title": "Tour Down Under" }, etc.
    
Please Sign in or register to post replies

Write your reply to:

Draft