Copied to clipboard

Flag this post as spam?

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


  • molu 4 posts 75 karma points c-trib
    Nov 03, 2021 @ 10:47
    molu
    0

    GetBlockListHtml from Model.Root

    Hi,

    Having a site with a BlockList property called topNotifications in the root of the content tree. I'm trying to access the blocks on all subpages. The BlockList holds blocks called notificationElement

    It is important that the blocks should be rendered using the template Partials/BlockList/topNotifications.cshtml

    and not the default Partials/BlockList/Default.cshtml


    In my layout Master.cshtml

            var rootnode = Model.Root();
            @Html.GetBlockListHtml(rootnode.TopNotifications, "topNotifications")
    

    and in Partials/BlockList/topNotifications.cshtml

    @inherits UmbracoViewPage<BlockListModel>
    @using Umbraco.Core.Models.Blocks
    <h1>BlockLists/Components/topNotifications.cshtml</h1>
    

    But i'm getting an error:

    Compiler Error Message: CS1061: 'IPublishedContent' does not contain a definition for 'TopNotifications' and no accessible extension method 'TopNotifications' accepting a first argument of type 'IPublishedContent' could be found (are you missing a using directive or an assembly reference?)
    

    I'm relatively new to Umbraco, so please bear with me, if there's an obvious reason for the error. 😬

    Thanks

  • Ambert van Unen 175 posts 817 karma points c-trib
    Nov 04, 2021 @ 19:57
    Ambert van Unen
    0

    Hi Molu,

    Model.Root() most likely return an type of IPublishedContent. This does not have a property 'TopNotifications'. (Basically exactly what the error says, you can easily see this if you use VisualStudio's intellisense for example), however it has other properties that IPuplishedContent has (For example Name).

    Your Rootnode is probably of a type of something like HomePage, which is based on IPublishedContent, but has extra properties. The code currently does not know these properties, it only knows that var rootnode = Model.Root(); returns a IPublishedContent type.

    I'm not sure if you are using Modelsbuilder or not, but if you do you could cast it likes : var rootnode = (HomePage)Model.Root(); This way, the code knows its no longer of a type IPublishedContent, but of type HomePage and you can use rootnode.TopNotifications (Use the type of your root node),

    Or if you do not use ModelsBuilder you could use the method like this:

    @Html.GetBlockListHtml(Model, "TopNotifications")
    

    For reference, check out this: https://our.umbraco.com/Documentation/Fundamentals/Backoffice/Property-Editors/Built-in-Property-Editors/Block-List-Editor/index-v8 It's mostly explained here how to render it.

    Edit: Now that I read it again, you most like need use the @Html.GetBlockListHtml(Model, "TopNotifications"). I've added some explanation as to why the error says what it says.

    Hope this helps!

Please Sign in or register to post replies

Write your reply to:

Draft