Copied to clipboard

Flag this post as spam?

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


  • jake williamson 207 posts 873 karma points
    Nov 10, 2017 @ 14:33
    jake williamson
    0

    is it possible to access the 'key' value from a nested content item?

    hey out there,

    something i wanted to check: is it possible to access the 'key' value from a nested content item?

    i've been debugging my code and i can see this coming back for my nested content item:

    [{
        "key": "8b7e1484-834a-43e5-9707-d528cd83a984",
        "name": "item one",
        "ncContentTypeAlias": "myAlias",
        "title": "item one title",
        "price": "150.00"
    }]
    

    using something like this:

    @foreach (var t in Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("myAlias"))
    {
        foreach (var publishedProperty in t.Properties)
        {
            @(publishedProperty.Value)<br/>
        }
    }
    

    it only outputs the 'title' and 'price' values...

    i really need the 'key' as i'm working on the basis this is the unique id for the item and we're using it as a selection in a form.

    or am i chasing the wrong monkey up the wrong tree?!

    cheers,

    jake

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Nov 10, 2017 @ 14:44
    Dan Diplo
    102

    You should be able to use the GetKey() extension method in Umbraco.Web namespace. So:

    @foreach (var t in Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("myAlias"))
    {
        <p>@t.GetKey()</p>
    }
    

    This is assuming you are using a recent version of Umbraco.

  • jake williamson 207 posts 873 karma points
    Nov 10, 2017 @ 14:48
    jake williamson
    0

    boom! that's epic and works a treat ;)

    now all i gotta do is get that working with ditto! i'm thinking i'm gonna have to build a custom processor as it's defo not doing it out of the box...

    thanks dan, top work.

Please Sign in or register to post replies

Write your reply to:

Draft