I have a node with a few child nodes. i would like to render content from a node between the first and last node.
i am able to get content from both first and last child nodes using the following code
@{ var firstChild = @Model.Content.Children.First();}
@{ var lastChild = @Model.Content.Children.Last();}
But when i try the following code i get YSOD
@{ var nthChild = @Model.Content.Children.Next(1);}
Am i missing some thing ?
Any assistance would be great
So the Next() method is an extension method for a singe IPublishedContent model, and it returns the node next to the current model in the tree.
Your are trying to use it on a List of IPublishedContent, and that does not work.
Instead if you browse a child node and have this in your view: @Model.Content.Next(), that would return the next child after your current browsing child.
Make sure however to null check, because I think the .Next() method throws a null reference exception if the child your browsing does not have a next child or is the only child for instance.
Traversing - get next node
Dear Umbraco Team
I have a node with a few child nodes. i would like to render content from a node between the first and last node. i am able to get content from both first and last child nodes using the following code
But when i try the following code i get YSOD
Am i missing some thing ? Any assistance would be great
Thanks Dibs
You can use the Siblings() extension.
Or you could use FollowingSibling which has a few overloads.
@Model.Content.FollowingSibling("textPage",true)
The gets the next sibling, and if you're on the last one, it'll wrap round to the first.
Or something like this...
Hi Dibs.
So the Next() method is an extension method for a singe IPublishedContent model, and it returns the node next to the current model in the tree.
Your are trying to use it on a List of IPublishedContent, and that does not work.
Instead if you browse a child node and have this in your view: @Model.Content.Next(), that would return the next child after your current browsing child.
Make sure however to null check, because I think the .Next() method throws a null reference exception if the child your browsing does not have a next child or is the only child for instance.
Hopefully this makes sense.
Also have a look at the Querying/Traversing documentation, that might help you: https://our.umbraco.org/documentation/reference/templating/mvc/querying
Best of luck!
Cheers Paul & Dennis
While you were giving me feedback, I got the following to work.
@{ var child = @Model.Content.Children.First();}
@child.Next(nth).GetPropertyValue("property")
Thanks Dibs
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.