Reputation: 669
I've recently started working with Umbraco and I'm trying to get the last child from an array. This is what I have done so far:
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{var page = Umbraco.Content(1127).Children[Children.Length - 1]}
<h5><a href='@page.Url'>@page.NewsTitle</a></h5>
<p>@page.NewsIntro</p>
<p class='read-more'><a href='@page.Url'>Read more...</a></p>
Edit:
Solution is as follows:
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{
var length = Umbraco.Content(1127).Children.Count();
var page = Umbraco.Content(1127).Children[length - 1];
}
<h5><a href='@page.Url'>@page.NewsTitle</a></h5>
<p>@page.NewsIntro</p>
<p class='read-more'><a href='@page.Url'>Read more...</a></p>
Upvotes: 2
Views: 1340
Reputation: 1516
Umbraco has a bunch of "isHelpers" including IsLast(). Here is an exanple using isLast.
foreach(var page in Umbraco.Content(1127).Children)
{
if(page.IsLast())
{
<h5><a href='@page.Url'>@page.NewsTitle</a></h5>
<p>@page.NewsIntro</p>
<p class='read-more'><a href='@page.Url'>Read more...</a></p>
}
}
Here are some of the other isHelpers that Umbraco exposes.
IsHelper Methods .IsFirst([string valueIfTrue][,string valueIfFalse]) Test if current node is the first item in the collection
.IsNotFirst([string valueIfTrue][,string valueIfFalse]) Test if current node is not first item in the collection
.IsLast([string valueIfTrue][,string valueIfFalse]) Test if current node is the last item in the collection
.IsNotLast([string valueIfTrue][,string valueIfFalse]) Test if current node is not the last item in the collection
.IsPosition(int index[,string valueIfTrue][,string valueIfFalse]) Test if current node is at the specified index in the collection
.IsNotPosition(int index[,string valueIfTrue][,string valueIfFalse]) Test if current node is not at the specified index in the collection
.IsModZero([string valueIfTrue][,string valueIfFalse]) Test if current node position evenly dividable (modulus) by a given number
.IsNotModZero([string valueIfTrue][,string valueIfFalse]) Test if current node position is not evenly dividable (modulus) by a given number
.IsEven([string valueIfTrue][,string valueIfFalse]) Test if current node position is even
.IsOdd([string valueIfTrue][,string valueIfFalse]) Test if current node position is odd
.IsEqual(IPublishedContent otherNode[,string valueIfTrue][,string valueIfFalse]) Tests if the current node in your iteration is equivalent (by Id) to another node
.IsDescendant(IPublishedContent otherNode[,string valueIfTrue][,string valueIfFalse]) Tests if the current node in your iteration is a descendant of another node
.IsDescendantOrSelf(IPublishedContent otherNode[,string valueIfTrue][,string valueIfFalse]) Tests if the current node in your iteration is a descendant of another node or is the node
.IsAncestor(IPublishedContent otherNode[,string valueIfTrue][,string valueIfFalse]) Tests if the current node in your iteration is an ancestor of another node
.IsAncestorOrSelf(IPublishedContent otherNode[,string valueIfTrue][,string valueIfFalse]) Tests if the current node in your iteration is an ancestor of another node or is the node
Upvotes: 0
Reputation: 1437
If you're able to use Linq in your C# code I see there, it's got a "Last()" extension method you could use.
Like this:
@using System.Linq
@{var page = Umbraco.Content(1127).Children.Last()}
Upvotes: 3
Reputation: 32780
Try: @{var page = Umbraco.Content(1127).Children[Children.Length - 1]}
Do note the upper cased L
in Length
. c# is case sensitive, there is no array property named length
.
Upvotes: 4