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 261 posts 1002 karma points c-trib
    Apr 13, 2022 @ 10:24
    Martin Rud
    0

    Block list loop gives unhandled error

    Hi forum,

    When trying to loop a block list property I get this error (attached image shows more):

    An unhandled exception occurred while processing the request.
    ModelBindingException: Cannot bind source type Umbraco.Cms.Core.Models.Blocks.BlockListItem`2[[Umbraco.Cms.Web.Common.PublishedModels.SiteBlItem_TextImage, ModelsGeneratedAssembly, Version=0.0.0.2, Culture=neutral, PublicKeyToken=null],[Umbraco.Cms.Core.Models.PublishedContent.IPublishedElement, Umbraco.Core, Version=9.4.3.0, Culture=neutral, PublicKeyToken=null]] to model type Umbraco.Cms.Core.Models.PublishedContent.IPublishedContent.
    Umbraco.Cms.Web.Common.ModelBinders.ContentModelBinder.ThrowModelBindingException(bool sourceContent, bool modelContent, Type sourceType, Type modelType)
    

    This is my partial view code:

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
    @using Umbraco.Cms.Core.Models.Blocks;
    @{
        var blocks = Model.Value<IEnumerable<BlockListItem>>("ribbons");
        foreach (var block in blocks)
        {
            var content = block.Content;
    
            <h2>@content.Value("headline")</h2>
            @await Html.PartialAsync("SiteBlocklist/" + content.ContentType.Alias, block)
        }
    }
    

    The partials called above does reside in the correct folder (I believe):

    enter image description here

    Full error:

    enter image description here

  • Damian 69 posts 392 karma points
    Apr 13, 2022 @ 19:40
    Damian
    100

    Hi Martin, It's a bit difficult to get you an exact answer without seeing the view for SiteBlocklist/SiteBlItem_TextImage but on that view, i would expect to see something like the following:

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Umbraco.Cms.Core.Models.Blocks.BlockListItem>;
    @{
        var block = (SiteBlItem_TextImage) Model.Content;
    }
    @*do stuff here*@
    

    It seems that view is trying to bind to a different model altogether, say SiteBlItem_YouTubeVideo (for example) instead of SiteBlItem_TextImage

    Hope this helps get on track. If not, please post that other view and we'll get it figured out.

  • Martin Rud 261 posts 1002 karma points c-trib
    Apr 14, 2022 @ 09:42
    Martin Rud
    0

    Cool! And thanks! That was the solution. :)

    In the view for SiteBlocklist/SiteBlItem_TextImage I now have:

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Umbraco.Cms.Core.Models.Blocks.BlockListItem>;
    @{
        var content = Model.Content;
    }
    <p>@content.Value("headline")</p>
    
Please Sign in or register to post replies

Write your reply to:

Draft