Copied to clipboard

Flag this post as spam?

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


  • Joshua Collis 4 posts 85 karma points
    Aug 12, 2019 @ 15:40
    Joshua Collis
    0

    Can't get strongly typed Model.Content.GetPropertyValue to be recursive

    OK so here goes, I have created a macro partial with the intent of grabbing a "Useful links" field and placing it on the page. The field in question is the Umbraco multi url picker.

    I want this to be able to be placed on any page and grab the links from the closest parent.

    What's strange is that this works perfectly fine when the macro is rendered using @Umbraco.RenderMacro and the recursive function works. It does NOT work when the macro is included via the grid editor or rich text editor. [EDIT] To be clear, I mean that on public pages where the macro was included in this way, the macro does not appear to have grabbed the links from the parent page. I see the title from within the macro but no links are rendered beneath it.

    Here is the code for my macro partial. I am using the latest version of Umbraco 7 to date (not ready to take the plunge to 8 for this project as the client is very resistant to change sadly)

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco.Web.Models
    
    <div id="usefulLinks">
        <div class="page-header">
            <h2>Useful links</h2>
        </div>
    
        @{
            var links = Model.Content.GetPropertyValue<IEnumerable<Link>>("usefulLinks", true);
            if (links.Any())
            {
                <ul style="padding-left: 2em;">
                    @foreach (var link in links)
                    {
                        <li><h5><a href="@link.Url" target="@link.Target">@link.Name</a></h5></li>
                    }
                </ul>
            }
        }
    </div>
    
  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Aug 13, 2019 @ 06:17
    Shaishav Karnani from digitallymedia.com
    100

    Hi Joshua,

    This is bug from Umbraco.

    If you try Model.Content.HasValue("usefulLinks")

    It will always return True even when "usefulLinks" is empty.

    So, I solved by applying this approach as you are looking only closest parent to return result. Infact, recursive : true is not required.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco.Web.Models
    
    <div id="usefulLinks">
        <div class="page-header">
            <h2>Useful links</h2>
        </div>
    
        @{
            var links = Model.Content.GetPropertyValue<IEnumerable<Link>>("usefulLinks", true);
            if (!links.Any())
            {
                links = Model.Content.Parent.GetPropertyValue<IEnumerable<Link>>("usefulLinks", true);
            }
    
            if (links.Any())
            {
                <ul style="padding-left: 2em;">
                    @foreach (var link in links)
                    {
                        <li><h5><a href="@link.Url" target="@link.Target">@link.Name</a></h5></li>
                    }
                </ul>
            }
        }
    </div>
    

    Hope that helps.

  • Joshua Collis 4 posts 85 karma points
    Aug 13, 2019 @ 11:55
    Joshua Collis
    0

    Thank you so much for this! solved my problem perfectly :)

  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Aug 13, 2019 @ 11:44
    Shaishav Karnani from digitallymedia.com
    0

    I have tried that this issue is fixed on v8. So, this bug is v7 specific only.

  • Joshua Collis 4 posts 85 karma points
    Aug 13, 2019 @ 12:33
    Joshua Collis
    0

    For anybody who is interested, I wrote a helper method based on DigitallyMedia's answer above that will allow for any number of levels to be recursed correctly

    public static IEnumerable<T> GetItems<T>(this IPublishedContent content, string field, bool recursive = false)
    {
        var items = content.GetPropertyValue<IEnumerable<T>>(field);
    
        If (recursive)
        {
            // Recurse if there are no links and there is a parent node
            if (!items.Any() && content.Parent != null)
            {
                return content.Parent.GetItems<T>(field, recursive);
            }
        }
    
        return items;
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft