Been trying to find the last related link within a foreach loop. But there doesn't seem to be any Last() or IsLast() methods available. Does anyone know a way around this without having to use a count loop. Code and error below:
@{
if (root.HasValue("relatedLinks") && root.relatedLinks.ToString().Length > 2)
{
<ul>
@foreach (var item in root.relatedLinks)
{
var test = root.relatedLinks;
var linkUrl = (bool)item.isInternal ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.link;
var linkTarget = (bool)item.newWindow ? "_blank" : null;
<li><a href="@linkUrl" target="@linkTarget">@item.caption</a></li>
if (item.IsLast()) { <li>Do this</li>}
}
</ul>
}
'Newtonsoft.Json.Linq.JObject' does not contain a definition for 'IsLast'.
Just done a bit more research and developed the following solution but I still think there could be better solution without having to go through two loops.
@{
if (root.HasValue("relatedLinks") && root.relatedLinks.ToString().Length > 2)
{
dynamic jObj = Newtonsoft.Json.JsonConvert.DeserializeObject(root.relatedLinksToString());
int count = jObj.Count;
int loopcount = 0;
<ul>
@foreach (var item in root.relatedLinks)
{
var linkUrl = (bool)item.isInternal ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.link;
var linkTarget = (bool)item.newWindow ? "_blank" : null;
<li><a href="@linkUrl" target="@linkTarget">@item.caption</a></li>
loopcount++;
if (loopcount == count) { } else {<li>|</li> }
}
</ul>
}
Getting Last Item in related links (Umbraco 7)
Been trying to find the last related link within a foreach loop. But there doesn't seem to be any Last() or IsLast() methods available. Does anyone know a way around this without having to use a count loop. Code and error below:
'Newtonsoft.Json.Linq.JObject' does not contain a definition for 'IsLast'.
Thanks in advance for your help.
Just done a bit more research and developed the following solution but I still think there could be better solution without having to go through two loops.
}
Hi, I would do something like this:
Jeavon
is working on a reply...