Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bezmundo 48 posts 139 karma points
    May 09, 2014 @ 12:04
    Bezmundo
    0

    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:

    @{
    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'.

    Thanks in advance for your help.

  • Bezmundo 48 posts 139 karma points
    May 09, 2014 @ 12:24
    Bezmundo
    1

    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>
    }
    

    }

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    May 11, 2014 @ 22:19
    Jeavon Leopold
    0

    Hi, I would do something like this:

    @{
        if (Model.Content.HasValue("relatedLinks") && Model.Content.GetPropertyValue<string>("relatedLinks").Length > 2)
        {
            <ul>
                @{
                    var relatedLinks = Model.Content.GetPropertyValue<JArray>("relatedLinks");
                    foreach (var item in relatedLinks)
                    {
                        var linkUrl = (item.Value<bool>("isInternal")) ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.Value<string>("link");
                        var linkTarget = item.Value<bool>("newWindow") ? "_blank" : null;
                        <li><a href="@linkUrl" target="@linkTarget">@(item.Value<string>("caption"))</a></li>
                        if (!item.Equals(relatedLinks.Last))
                        {
                            <li>|</li>
                        }
                    }
                }
            </ul>
        }
    } 
    

    Jeavon

Please Sign in or register to post replies

Write your reply to:

Draft