Copied to clipboard

Flag this post as spam?

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


  • Ras 66 posts 330 karma points
    Jun 02, 2022 @ 08:16
    Ras
    0

    Working with nested content with multiple element types

    Hi, having a bit of trouble with working with a nested content picker that has three different elements that can be added. The elements contain different datetype properties so I need to be able to be able to access them and sort them somehow (that's a different issue).

    Is there a way to get a typed object of my picker so that I'm able to access all the different elements it may contain? Both in the razor view and in an API.

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Jun 02, 2022 @ 08:37
    Chriztian Steinmeier
    0

    Hi Ras,

    For the Razor part, I usually let .NET do this for me - e.g.:

    The picker returns an IEnumerable<IPublishedElement> which I send into a RenderBlocks partial:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IEnumerable<IPublishedElement>>
    @{
        var blocks = Model;
    }
    @foreach (var block in blocks) {
        @Html.Partial("Blocks/" + block.ContentType.Alias, block)
    }
    

    This allows you to create a partial for each block type that takes the block's type as input - .NET does the casting for you, e.g. if I have an AddressCard element type, I'd have an /Views/Partials/Blocks/AddressCard.cshtml partial like this (simplified output for brevity):

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<AddressBlock>
    @{
        var block = Model;
        var locationName = block.AddrLocation;
    }
    <div class="address h-card">@(locationName)</div>
    

    Hope that helps,

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft