Copied to clipboard

Flag this post as spam?

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


  • Fernando Susena 6 posts 56 karma points
    Jan 28, 2014 @ 18:29
    Fernando Susena
    0

    Recursive Related Links with Razor

    Hi Guys, I'm using Umbraco 4.9.1 trying to make a Related Links Razor Macro Recursive throughout the entire site.

    With XSLT or Common nodes this was simple enough, but I can't get it to work on Razor, I'm using the default Related Links code and it works great on the homepage, but it doesn't show the Related links nowhere else on the site, just the homepage.

    Here's my Razor Code:

    @{
    if (Model.HasProperty("footerAccion")) {
    <ul>
        @foreach (var link in @Model.FooterAccion) {
            string target = link.newwindow == "1" ? " target=\"_blank\"" : "";
            <li>
                @if (link.type == "internal") {
                    <a href="@umbraco.library.NiceUrl(int.Parse(link.link))"@Html.Raw(target)>@link.title</a>
                } else {
                    <a href="@link.link"@Html.Raw(target)>@link.title</a>
                }
            </li>
        }
    </ul>
    }
    

    This works great on the homepage, but when I navigate throughout the site, even though these macros are on the master template and should be present everywhere, they appear empty. I tried solutions with AncestorOfSelf and a few other suggestions here, but no avail.

    Anyone has any ideas? Thanks!

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Jan 28, 2014 @ 18:40
    Dennis Aaen
    1

    Hi Fernando,

    Try this:

    @{
    if (Model.HasProperty("footerAccion")) {
    <ul>
        @foreach (var link in Model._FooterAccion) {
            string target = link.newwindow == "1" ? " target=\"_blank\"" : "";
            <li>
                @if (link.type == "internal") {
                    <a href="@umbraco.library.NiceUrl(int.Parse(link.link))"@Html.Raw(target)>@link.title</a>
                } else {
                    <a href="@link.link"@Html.Raw(target)>@link.title</a>
                }
            </li>
        }
    </ul>
    }

    The way that you can do a value recrusive, is by adding the underscore _ before the property. So Model._property. The documentation can be found here: http://our.umbraco.org/documentation/Reference/Querying/DynamicNode/Properties

    Hope this helps,

    /Dennis

  • Fuji Kusaka 2203 posts 4220 karma points
    Jan 28, 2014 @ 18:50
    Fuji Kusaka
    0

    Hi Fernando,

    You can make use of

    Model._propertyAlias

    to get a recursive value in razor

    //fuji

  • Fuji Kusaka 2203 posts 4220 karma points
    Jan 28, 2014 @ 18:51
    Fuji Kusaka
    0

    Ah @Dennis you already replied to this threat!!

  • Fernando Susena 6 posts 56 karma points
    Jan 28, 2014 @ 19:23
    Fernando Susena
    100

    Thanks for the replies! I'm a complete newbie with Razor.

    Your suggestion works if I remove the "If" that comes with the Related Links "Stock" Script. As a reference for anyone else with this issue, adding the "_" did the trick as you guys advised.

    Here is the working result:

    <ul>
        @foreach (var link in @Model._footerAccion) {
            string target = link.newwindow == "1" ? " target=\"_blank\"" : "";
            <li>
                @if (link.type == "internal") {
                    <a href="@umbraco.library.NiceUrl(int.Parse(link.link))"@Html.Raw(target)>@link.title</a>
                } else {
                    <a href="@link.link"@Html.Raw(target)>@link.title</a>
                }
            </li>
        }
    </ul>
    

    Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft