Copied to clipboard

Flag this post as spam?

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


  • Sam 23 posts 115 karma points
    Jan 30, 2020 @ 15:55
    Sam
    0

    Umbraco 8 Modular Nested Content

    Hello,

    In V7, i would have a view, that took nested content and looped through them looking for a Document Type Alias and matching it to a Partial view name. That view would then be rendered out.

    In V8 i cannot get it to do the same thing as i cannot pass the data (contentModule) from the Nested Content Document type in the loop, to the partial.

    Is there a way to do this or better in V8?

    code:

    Layout = "_MasterLayout.cshtml";
    
    //Nested Content Picker
    var teamPicker = Model.TeamPicker;
    
    foreach (var contentModule in teamPicker)
    {
    
         @Html.Partial("~/Views/Partials/NestedContent/" + contentModule.ContentType.Alias + ".cshtml", contentModule)
    
    }
    

    TIA, Sam

  • Ambert van Unen 175 posts 819 karma points c-trib
    Jan 31, 2020 @ 07:43
    Ambert van Unen
    100

    Doing this out of my head, but I believe you could do something like this.

    foreach(var contentModule in teamPicker)
    {
        @RenderPartial(contentModule);
    }
    
    @helper RenderPartial(NestedNodePickerType module){
        @Html.Partial("~/Views/Partials/NestedContent/" + module.ContentType.Alias + ".cshtml", module)
    }
    
    @helper RenderPartial(OtherNestedNodePickerType module){
        @Html.Partial("~/Views/Partials/NestedContent/" + module.ContentType.Alias + ".cshtml", module)
    }
    

    Or perhaps with a switch /case?

     foreach(var contentModule in teamPicker)
    {
     switch (contentModule.Alias)
        {
            case "nestedContentType":
                @Html.Partial("~/Views/Partials/NestedContent/" + contentModule.ContentType.Alias + ".cshtml", contentModule)
                break;
            case "otherNestedContentType":
                @Html.Partial("~/Views/Partials/NestedContent/" + contentModule.ContentType.Alias + ".cshtml", contentModule)
                break;
    
        }
    
    }
    
  • Magnus Eriksson 122 posts 362 karma points
    Jan 31, 2020 @ 08:26
    Magnus Eriksson
    0

    I use this pattern, works in v8 with some minor changes (IPublishedContent is now IPublishedElement): https://gist.github.com/ChesterCampbellAM/2e9e14de81a4ee242eee46ce6c4936e4

    Regards, Magnus

  • Sam 23 posts 115 karma points
    Jan 31, 2020 @ 16:04
    Sam
    1

    Hi Guys,

    Thank you both for your input it's helped greatly!

    I tried several ways and managed to settle on:

    Template:

    //Nested Content Picker
    var teamPicker = Model.Value<IEnumerable<IPublishedElement>>("teamPicker");
    
    foreach (var contentModule in teamPicker)
    {
        @Html.Partial("~/Views/Partials/NestedContent/" + contentModule.ContentType.Alias + ".cshtml", contentModule)
    }
    

    Partial:

    var teamModel = Model.Properties;
    foreach (var item in teamModel)
    {
    <h1>@item.Value("teamTitle")</h1>
    
    }
    

    This article also helped: https://our.umbraco.com/forum/templates-partial-views-and-macros/97248-nested-content-in-separate-partials

    @ Marcus - I will try your method too as im keen to learn other ways :)

    Many thanks again guys.

Please Sign in or register to post replies

Write your reply to:

Draft