jsonrazorcdataumbraco7umbraco-mvc

Umbraco 7.2.8 related links razor display


I have a document type which contains related links in Umbraco 7.2.8

I want to display it in a view but HasValue comes as false and GetPropertyValue("relatedLinks") come sempty as well.

So I took a look at the node in the database and the property "relatedLinks" is written as CData, not JSON:

  <relatedLinks><![CDATA[[
  {
    "caption": "Some caption",
    "link": 1163,
    "newWindow": false,
    "internal": 1163,
    "edit": false,
    "isInternal": true,
    "internalName": "Caption",
    "type": "internal",
    "title": "Link Title"
  }
]]]></relatedLinks>

As far as I know in this umbraco version it should be written as JSON (eg. as per example here https://our.umbraco.org/forum/developers/razor/54450-Related-Links-Razor-Macro-Umbraco-7) and then easy to display on a view using Razor.

Not sure how I am supposed to use CDATA snippet in Razor?

Or how to force the cms to write it as JSON?


Solution

  • Have you tried using it as a JArray?

    @using Newtonsoft.Json.Linq
    @{      
        if (Model.Content.HasValue("relatedLinks") && Model.Content.GetPropertyValue<string>("relatedLinks").Length > 2)
        {
            <ul>
                @foreach (var item in Model.Content.GetPropertyValue<JArray>("relatedLinks"))
                {
                    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>
        }
    }  
    

    https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/related-links