Copied to clipboard

Flag this post as spam?

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

  • bh 376 posts 1320 karma points
    Oct 21, 2020 @ 14:37

    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 4013 posts 15773 karma points MVP 12x admin c-trib
    Oct 21, 2020 @ 15:10
    Lee Kelleher

    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.

    - Lee

Please Sign in or register to post replies

Write your reply to: