Copied to clipboard

Flag this post as spam?

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


  • bh 291 posts 976 karma points
    Oct 21, 2020 @ 14:37
    bh
    0

    Partial inside a foreach

    On my docType I have a nestedcontent. The type of that nested content is NcHeroShadowBox.

    @foreach(NcHeroShadowBox pillar in Model.GetPropertyValue<IEnumerable<IPublishedContent>>("diversityPillars")) { 
        @Html.Partial("ncHeroShadowBox", pillar);    
    }
    

    I'm getting this error on the code above:

    Cannot bind source type NcHeroShadowBox to model type System.Collections.Generic.IEnumerable

  • Lee Kelleher 3945 posts 15163 karma points MVP 10x admin c-trib
    Oct 21, 2020 @ 15:10
    Lee Kelleher
    100

    Hi bh,

    The issue is to do with casting the pillar object to the type that the partial view is expecting. (Depending on your understanding of C# that will either sound fine, or a bit confusing.)

    Either way, in your "ncHeroShadowBox.cshtml" partial-view file, it should start with the line...

    @inherits UmbracoViewPage<...>
    

    The bit in the angle brackets is the object-type for that this partial-view expects to use as it's @Model. Looking at your error, it appears to start with IEnumerable<...>.

    If you swap that to be...

    @inherits UmbracoViewPage<NcHeroShadowBox>
    

    ...then hopefully that should solve it.

    Cheers,
    - Lee

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies