2
votes

How can I get all the children for the News item (in the standard project) in a partialview (razor)?

I've tried:

@{
    var homePage = CurrentPage.AncestorsOrSelf(1).First();
    var newsItems = homePage.Children.Where(x => x.GetProperty("Name").Value == "News");
}

But I get an error that states I can't do lambda expression, whitout casting it. "News" is a node in my webpage holding children and I want to create a macro listing the children. How and what can I cast it to?

2

2 Answers

1
votes

Currently, you are looking for children of the home page, named "News". I think you want to go one level deeper.

I reccomend this approach:

// 1- Get root node
var site = Model.Content.AncestorOrSelf("Site");

// 2- Get news node
var news = site.Descendant("News");
var newsItems = news.Children;

Here you use the document type alias to traverse your tree, this is much more reliable than using names, as those can change. This of course may require to rework some elements.

Hope this helps!

0
votes

Try this to get all of the news nodes that are children of homepage:

var newsItems = homePage.Children.Where("Name = @0", "News");

Then iterate through the children of News:

foreach (var newsChild in newsItems)

You could skip the first step if you already know the ID of your news node like so:

var newsNode = Umbraco.Content(1234);

This page has plenty of examples: http://our.umbraco.org/documentation/reference/querying/DynamicNode/Collections#