Copied to clipboard

Flag this post as spam?

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


  • Martin Rud 231 posts 901 karma points c-trib
    Nov 23, 2020 @ 11:02
    Martin Rud
    0

    .Name gives "cannot convert from 'method group' to 'HelperResult'"

    On a document type I have a nested content property "ribbons". One type of ribbon build upon a document type with one field "ribbonItems" of type Multi Node Tree Picker where editors can choose nodes to list in the ribbon.

    Below are two versions of my code (some null checks and other stuff is removed for clarity). It is the latter that I would like to get working:

    Works, but does not include fetching node name:

    var ribbons = Model.Value<IEnumerable<IPublishedElement>>("ribbons").Where(x => x.IsVisible());
    foreach (var ribbon in ribbons)
    {
        var ribbonItems = ribbon.Value<IEnumerable<IPublishedElement>>("ribbonItems");
        if (ribbonItems != null && ribbonItems.Any())
        {
            foreach (var ribbonItem in ribbonItems)
            {
                <h2>@ribbonItem.Value("headline")</h2>
                @ribbonItem.Value("text")
            }
        }
    }
    

    Works, but gives error:

    var ribbons = Model.Value<IEnumerable<IPublishedElement>>("ribbons").Where(x => x.IsVisible());
    foreach (var ribbon in ribbons)
    {
        var ribbonItems = ribbon.Value<IEnumerable<IPublishedElement>>("ribbonItems");
        if (ribbonItems != null && ribbonItems.Any())
        {
            foreach (var ribbonItem in ribbonItems)
            {
                <h2>@ribbonItem.Name</h2>
                @ribbonItem.Value("text")
            }
        }
    }
    

    The error: "CS1503: Argument 1: cannot convert from 'method group' to 'HelperResult'" for the line with "@ribbonItem.Name"

    How can I fetch the node name of a ribbonItem?

  • Marc Goodson 2126 posts 14218 karma points MVP 8x c-trib
    Nov 23, 2020 @ 11:11
    Marc Goodson
    1

    Hi Martin

    I think it's because it's an IPublishedElement you have here and IPublishedElement doesn't have a 'Name' property:

    https://github.com/umbraco/Umbraco-CMS/blob/3bfd9b71e290354744d677440983ecd06c5c0788/src/Umbraco.Core/Models/PublishedContent/IPublishedElement.cs

    (you'd expect it to if you have been working with V7 and IPublishedContent that does have that property!)

    But I think, because Element Types are created as part of Nested Content or Block editor, they don't really have a 'name' in the same way as a published item in the Content tree would.

    regards

    Marc

  • Nik 1591 posts 7148 karma points MVP 6x c-trib
    Nov 23, 2020 @ 11:32
    Nik
    101

    Hey Martin,

    According to your question, the property ribbonItems is a Multi Node Tree Picker (MNTP), but you are currently retrieving the contents as IEnumerable<IPublishedElement>, which as Marc mentions doesn't have a Name property.

    As a MNTP references real content nodes it actually returns IEnumerable<IPublishedContent> so if you switch over to that you should find that Name becomes available and is no longer an issue for you.

    Cheers

    Nik

  • Martin Rud 231 posts 901 karma points c-trib
    Nov 23, 2020 @ 11:39
    Martin Rud
    0

    Hi Marc and Nik,

    Thank you, both! :)

    I have marked Niks anwers as a solution since he answered first in the copy of mu question in the Umbraco Web Developers forum (https://www.facebook.com/groups/202933450108554/permalink/1183692555365967/)

Please Sign in or register to post replies

Write your reply to:

Draft