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"))
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.
Reverse reading of content in Razor
Hi folks,
I've got the following razor that reads in content from the tree
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
and
but it's not affecting the order.
Any ideas folks?
Thanks, Sx
Hi Craig
Maybe you need OrderByDescending?
Can you check createDates of child items in umbraco config file or in backend?
Thanks,
Alex
Hi Alex,
Sadly this doesn't work either...It appears that it should.
Craig
Craig, what about ordering by name? is it working?
Hi,
No that doesn't work either! Stumped!
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?
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
I think this is your issue:
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()
?Course it bloody does! What a dumbo I am.
Cheers me ole china!
is working on a reply...