Copied to clipboard

Flag this post as spam?

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


  • Craig O'Mahony 364 posts 918 karma points
    Apr 05, 2017 @ 08:16
    Craig O'Mahony
    0

    Reverse reading of content in Razor

    Hi folks,

    I've got the following razor that reads in content from the tree

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{var node = Umbraco.TypedContent(1134);}
    
    @{
        foreach (var child in node.Children)
        {
            if (child.Index() == 0) //Only get the first news item
            {
                <div class="col-sm-2">
                    <p>@child.CreateDate.ToString("dddd dd/MM/yy")</p>
                </div>
    
                <div class="col-sm-8 sm-no-padding">
    
                    @child.GetPropertyValue("bodyText")
                </div>
                break;
    
            }
        }
    }
    

    So this takes the first item from the tree and displays it onto the page, currently I'm having to create a piece of content and manually sort the content to get my latest item at the top. What I'd like to be able to do is reverse the order in which the content is read to save me this task.

    I've tried

    (var child in node.Children.OrderBy("CreateDate"))
    

    and

    (var child in node.Children.Reverse())
    

    but it's not affecting the order.

    Any ideas folks?

    Thanks, Sx

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Apr 05, 2017 @ 10:16
    Alex Skrypnyk
    0

    Hi Craig

    Maybe you need OrderByDescending?

    Can you check createDates of child items in umbraco config file or in backend?

    foreach (var child in node.Children.OrderByDescending(x => x.CreateDate))
    

    Thanks,

    Alex

  • Craig O'Mahony 364 posts 918 karma points
    Apr 05, 2017 @ 11:59
    Craig O'Mahony
    0

    Hi Alex,

    Sadly this doesn't work either...It appears that it should.

    Craig

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Apr 05, 2017 @ 12:32
    Alex Skrypnyk
    0

    Craig, what about ordering by name? is it working?

    foreach (var child in node.Children.ToList().OrderByDescending(x => x.Name))
    
  • Craig O'Mahony 364 posts 918 karma points
    Apr 05, 2017 @ 14:32
    Craig O'Mahony
    0

    Hi,

    No that doesn't work either! Stumped!

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Apr 05, 2017 @ 14:33
    Alex Skrypnyk
    0

    Craig, be sure that content is not reordered by js in the fronend, look please at source code of the page, I think it can be a problem.

    Did you try to debug this code?

  • Craig O'Mahony 364 posts 918 karma points
    Apr 05, 2017 @ 15:35
    Craig O'Mahony
    0

    Hi,

    There's no reordering on the front end, it simply takes what the razor returns and displays it. I just can't get the razor to return the correct content node. I think that I might have to write a hack in which I count the children of the parent and get the last one. Grim but I'm outta ideas.

    Thanks, C

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Apr 05, 2017 @ 16:22
    Nicholas Westby
    1

    I think this is your issue:

    child.Index() == 0
    

    I think that function gets the index of the content node in the content tree (rather than in the collection you are currently working with).

    I think you can skip the loop and this index check entirely. Why not just sort the items however you want, then call .FirstOrDefault()?

  • Craig O'Mahony 364 posts 918 karma points
    Apr 05, 2017 @ 16:27
    Craig O'Mahony
    0

    Course it bloody does! What a dumbo I am.

    Cheers me ole china!

Please Sign in or register to post replies

Write your reply to:

Draft