So I have been working with nested content for a while now. And I am wondering how you guys handle using multiple item. I normally have a partial view called "nested content" and a switch statement, and us it like this for eg.
@foreach(var item in items) {
switch(item.ContentType.Alias){
case "foo":
<h1>Bar</h1>
break;
}
}
But I have stared to wonder if there is a better solution than this. Maybe having the switch statement render a partial view with the html in it?
How do you normally work with multiple nested content items?
I tend to setup nested content to by generic, just saves a bit of overhead when adding new nested content items, and by this I do the following:
I have a document called 'Standard Content Page'
This document has the nested content on it
On the template have a partial called the following: @Html.Partial("~/Views/Partials/Content/Content.cshtml", Model.MainContent)
Inside of this partial, I have just this:
@model IEnumerable<IPublishedElement>
@{
if (Model.IsNullOrEmpty())
{
return;
}
}
@foreach (var nestedContent in Model)
{
@Html.Partial($"~/Views/Partials/Content/{nestedContent.ContentType.Alias}.cshtml", nestedContent)
}
Just keeps things nice and tidy, also saves having to update a couple of places when you add a new nested content. Just need to add the partial and you're away!
Working with multiple nested content items
Hello all!
So I have been working with nested content for a while now. And I am wondering how you guys handle using multiple item. I normally have a partial view called "nested content" and a switch statement, and us it like this for eg.
But I have stared to wonder if there is a better solution than this. Maybe having the switch statement render a partial view with the html in it?
How do you normally work with multiple nested content items?
//Johannes
I tend to setup nested content to by generic, just saves a bit of overhead when adding new nested content items, and by this I do the following:
I have a document called 'Standard Content Page' This document has the nested content on it On the template have a partial called the following:
@Html.Partial("~/Views/Partials/Content/Content.cshtml", Model.MainContent)
Inside of this partial, I have just this:
Just keeps things nice and tidy, also saves having to update a couple of places when you add a new nested content. Just need to add the partial and you're away!
Thank you for the idea Ryan!
That's really solid!
//Johannes
is working on a reply...