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.
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>
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.
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: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):Hope that helps,
/Chriztian
is working on a reply...