Copied to clipboard

Flag this post as spam?

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


  • Jesper Ordrup 1019 posts 1528 karma points MVP
    May 25, 2014 @ 17:30
    Jesper Ordrup
    0

    U7 Razor Example on how to render Widget grid in template?

    How do get the selected nodeids ?

    I get as far as:

     

    var cellname = "c22";
    var bExists = CurrentPage.GetPropertyValue("widgets").ContainsKey(cellname);    

     

    which returns true when cell exists .. but how to get the data? I've tried:

      var nodeids1 = CurrentPage.GetPropertyValue<string>("widgets")[cellname];   var nodeids2 = CurrentPage.GetPropertyValue("widgets")[cellname];

      var nodeids3 = CurrentPage.GetPropertyValue<int>("widgets")[cellname];

    but no luck

    best
    Jesper

     

     

     

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    May 25, 2014 @ 18:17
    Matt Brailsford
    0

    Hey Jesper,

    It's stored as a dictionary of int arrays

    {
        "cell1":[1,2,3],
        "cell2":[2,3,4]
    }
    

    So to retrive the values you'd do something like

    var widgets = Model.Content.GetPropertyValue<IDictionary<string, IEnumerable<int>>("widgets");
    var nodeIds = widgets["cell1"];
    
    foreach(var id in nodeIds){
        // Do something with the node id
    }
    

    Hope this helps.

    Matt

  • Jesper Ordrup 1019 posts 1528 karma points MVP
    May 25, 2014 @ 20:43
    Jesper Ordrup
    0

    Thanks matt .. but this line

    Model.Content.GetPropertyValue<IDictionary<string,IEnumerable<int>>("widgets");

    doesnt compile. It says : is a 'type' but is used like a 'variable'. So I added a missing >

    var widgets = Model.Content.GetPropertyValue<IDictionary<string, IEnumerable<int> >>("widgets");

    But now widgets is null ..

    If I poke around in immediate window a little I can see:

    Model.Content.GetPropertyValue("widgets")
    Count = 1
        [0]: {[c22, System.Linq.Enumerable+WhereEnumerableIterator`1[Umbraco.Core.Models.IPublishedContent]]}

    Sorry to bother you ;)

    Suggestion: I think it would be a huge help if complex datatypes like Widget Grid had a helper class to make it super easy for smalldevs like me and nondevs to do pull this off. Great idea isnt it :D

    /Jspr

     

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    May 25, 2014 @ 21:53
    Matt Brailsford
    100

    Hey Jesper,

    You are right that it needed an extra >. For why you are getting null, it seems I made it a dictionary of IPublishedContent arrays not ints, so just change the generic int param to IPublishedContent instead and that should then work.

    I'll have a look at creating a helper class in the next release.

    Matt

  • Jesper Ordrup 1019 posts 1528 karma points MVP
    May 25, 2014 @ 22:28
    Jesper Ordrup
    0

    Hey Matt,

    Works. Thank you! https://www.youtube.com/watch?v=r13riaRKGo0&feature=kp

    For completeness:

        var cellLeft = "c21";    // id in table for content in cell 1 (left) in second row
    var cellRight = "c22"; // id in table for content in cell 2 (right) in second row
    var widgets = Model.Content.GetPropertyValue<IDictionary<string, IEnumerable<IPublishedContent>>>("widgets");
    var nodesRight = widgets[cellRight]; // get nodes
        foreach(var node in nodesRight){
        @Html.Partial("Partials/widget-" + node.DocumentTypeAlias, node) // render partial macros based on alias name
    }

     

    best
    Jesper 

Please Sign in or register to post replies

Write your reply to:

Draft