Copied to clipboard

Flag this post as spam?

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


  • Nicole Polet 50 posts 202 karma points
    Jul 25, 2016 @ 22:05
    Nicole Polet
    0

    Rendering Nested Content within Nested Content

    I am just starting to use Nested Content and have got my template working for my "level one" nested content. But I want to have nested content within nested content and I'm having trouble rendering the "second level" nested content on my template. Here's what I've done:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
    var items = Model.GetPropertyValue<IEnumerable<IPublishedContent>>("sections");
    
    foreach(var item in items)
    {
        <section class="section @item.GetPropertyValue("sectionClass")">
            <div class="contain-content clearfix">
                <article class="span-60
                @if (item.HasValue("alignContent"))
                    {
                        var preValue = Umbraco.GetPreValueAsString(item.GetPropertyValue<int>("alignContent"));
                        @preValue.ToLower()
                    }
                ">
                    <h1>@item.GetPropertyValue("sectionTitle")</h1>
                    @item.GetPropertyValue("sectionContent")
                    @{
                        var secondItems = item.GetPropertyValue<IEnumerable<IPublishedContent>>("widget");
                        foreach(var secondItem in secondItems) {
    
                        }
                    }
                </article>
            </div>
        </section>
    }
    

    I have a foreach loop for my first-level nested content (sections) which works fine, but then I've tried to add another foreach loop within that, to loop through my second-level nested content (widgets). This gives me an error: System.NullReferenceException: Object reference not set to an instance of an object. (referring to the line that contains the second foreach loop)

    Can anyone tell me what I'm doing wrong?

    Thanks in advance!

  • David Peck 687 posts 1863 karma points c-trib
    Jul 26, 2016 @ 07:41
    David Peck
    101

    Is it as simple as?

    @{
         var secondItems = item.GetPropertyValue<IEnumerable<IPublishedContent>>("widget");
         if(secondItems  != null) {
             foreach(var secondItem in secondItems.Where(item => item !=null)) {
                 ...
             }
         }
    }
    
  • David Peck 687 posts 1863 karma points c-trib
    Jul 26, 2016 @ 07:43
    David Peck
    0

    I guess your concern is why is it null in the first place? I've had problems with this and adding new fields without publishing the content.

  • Nicole Polet 50 posts 202 karma points
    Jul 27, 2016 @ 21:57
    Nicole Polet
    0

    Thanks so much David,

    That worked!

Please Sign in or register to post replies

Write your reply to:

Draft