Copied to clipboard

Flag this post as spam?

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


  • Johannes Lantz 156 posts 838 karma points c-trib
    Jan 19, 2020 @ 12:27
    Johannes Lantz
    0

    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.

    @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?

    //Johannes

  • Ryan Helmn 26 posts 188 karma points
    Feb 10, 2020 @ 15:11
    Ryan Helmn
    100

    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!

  • Johannes Lantz 156 posts 838 karma points c-trib
    Feb 10, 2020 @ 17:23
    Johannes Lantz
    0

    Thank you for the idea Ryan!

    That's really solid!

    //Johannes

Please Sign in or register to post replies

Write your reply to:

Draft