Copied to clipboard

Flag this post as spam?

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


  • Sean Dooley 289 posts 528 karma points
    Jul 13, 2011 @ 11:48
    Sean Dooley
    2

    Related links count

    In Razor, how do you check the number (count) of related links? For example,

    if(@Model.RelatedLinks.Count() > 0) { //do something }

    Thank you, in advance, for any help.

  • Lee Kelleher 4024 posts 15833 karma points MVP 13x admin c-trib
    Jul 13, 2011 @ 18:39
    Lee Kelleher
    2

    Hi Sean,

    The only way I've been able to do this with Umbraco 4.7.0 is so...

    <umbraco:Macro  runat="server" language="cshtml">
    @{
       int count = 0;
    }
    @foreach(var @item in Model.relatedLinks.link)
    {
        count++;
    }
    @count
    </umbraco:Macro>

    Ugly hack huh? I wouldn't suggest actually using this though... but it's the only way I could figure out how to get the count!

    I believe there have been hugely significant improvements to DynamicNode/DynamicXml in the upcoming Umbraco 4.7.1 release - hopefully there's a more elegant solution for this problem.

    Cheers, Lee.

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Jul 14, 2011 @ 09:31
    Sebastiaan Janssen
    2

    Yup, counting will be possible in 4.7.1. Just a few more weeks of patience, or use a nightly build. :)

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 17, 2013 @ 11:25
    Anthony Candaele
    0

    I tested this in Umbraco 4.11.3.1 and it works:

    @Model.RelatedLinks.Count() > 0

    Because I read that, for performance reason it's better to use the GetProperty("alias") on @Model, I tried:

    @Model.GetProperty("RelatedLinks").Count > 0

    but this generates an error.

    anyone knows why this fails?

    Thanks for your help,

    Anthony

Please Sign in or register to post replies

Write your reply to:

Draft