Copied to clipboard

Flag this post as spam?

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


  • Pat O'Callaghan 34 posts 56 karma points
    May 24, 2012 @ 14:15
    Pat O'Callaghan
    0

    Best way to get specific items from DynamicXml?

    Hey,

    I'm a bit of a newbie to Razor so not sure how this is done, any help would be appreciated. I have a list of nodes in the uComponents Multi Node Picker (alias: testimonials) which I grab using:

    @Model.testimonials

    Due to the nature of the design/HTML I've to spit out, I'd like to split the contents of this list into three separate variables and then iterate over each one separately in a @foreach. It would get messy if I had to process the whole list all in one go. I tried the code below but this generates an error "'umbraco.MacroEngines.DynamicXml' does not contain a definition for 'Take'" because I guess Take is only for Collections not DynamicXml.

    var columnOne = testimonials.Take(2);
    var columnTwo = testimonials.Skip(2).Take(2);
    var columnThree = testimonials.Skip(4).Take(1);

    I'm fairly confident I could come up with a solution in XSLT but am trying to learn the Razor way.

    Thanks,

    Pat

  • Douglas Ludlow 210 posts 366 karma points
    May 24, 2012 @ 15:18
    Douglas Ludlow
    0

    You would do something like this:

    <ul>
    foreach(var item in Model.testimonials)
    {
    <li><a href="@item.url">@item.linktitle</a></li>
    }
    </ul> 

    For a list of available properties for the url picker, see this post

  • Pat O'Callaghan 34 posts 56 karma points
    May 24, 2012 @ 16:17
    Pat O'Callaghan
    0

    Hey Douglas, thanks for the reply.

    That's what I'd do if I wanted to process all my nodes in one loop but I want to split the list up into 3 variables e.g. the first two list items into a variable named columnOne, items 3 and 4 into columnTwo and the remaining items into a variable named columnThree. Then I would loop through each of these variables individually.

    Thanks,

    Pat

  • Douglas Ludlow 210 posts 366 karma points
    May 24, 2012 @ 16:46
    Douglas Ludlow
    2

    Ah, I see. DynamicXml, unfortunately doesn't provide much flexibility. But you load the DynamicXml values into a List simular to this:

    List<dynamic> testimonials = new List<dynamic>();

    foreach (var item in Model.testimonials)
    {
    var i = new
    {
    Title = item.linktitle,
    Url = item.url
    };

    testimonials.Add(i);
    }

    var columnOne = testimonials.Take(2);
    var columnTwo = testimonials.Skip(2).Take(2);
    var columnThree = testimonials.Skip(4).Take(1);

    <ul>
    @foreach(var item in columnOne)
    {
    <li><a href="@item.Url">@item.Title</a></li>
    }
    </ul>

    ...

     

  • Pat O'Callaghan 34 posts 56 karma points
    May 25, 2012 @ 09:51
    Pat O'Callaghan
    0

    Thanks Douglas, that worked!!

  • Shannon Deminick 1525 posts 5271 karma points MVP 2x
    Nov 19, 2012 @ 22:32
    Shannon Deminick
    0

    There is a proper fix coming in 4.11:

    http://issues.umbraco.org/issue/U4-1207

Please Sign in or register to post replies

Write your reply to:

Draft