Copied to clipboard

Flag this post as spam?

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


  • Paul Wright 16 posts 97 karma points
    Aug 21, 2020 @ 13:31
    Paul Wright
    0

    Nested content within nested content

    Afternoon All,

    just wondernig if you can have a look at this please, Trying to load nested content within nested content.

    seem to able to see the first level but the second doesn't seem to locate any information:

       @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
    @if (Model.HasValue("parts"))
        {
            var items = Model.Value<IEnumerable<IPublishedElement>>("parts");
            foreach(var item in items)
            {
                <h3>@item.GetProperty("title").Value()</h3>
               <div>     
                @{
    
                        var secondItems = item.Value<IEnumerable<IPublishedElement>>("questions");
                        if(secondItems  != null) {
                            <h3>@item.GetProperty("sectionNumber").Value()</h3>
    
                        }
                }
              </div>  
    
            }
        }
    

    I keep on getting the error: Object reference not set to an instance of an object.

    here is the nested content: incase i'm entering the wrong details into the second one

    Here is the Nested content

  • Joep 96 posts 698 karma points
    Aug 21, 2020 @ 13:38
    Joep
    0

    Hi, well you should probably loop through you're secondItems list. Like this:

    var secondItems = item.Value<IEnumerable<IPublsihedElement>>("questions");
    if(secondItems.Any()){
    foreach(var secondItem in secondItems){
        <h3>@secondItem.GetProperty("sectionNumber").Value()</h3>
      }
    }
    
  • Paul Wright 16 posts 97 karma points
    Aug 21, 2020 @ 14:05
    Paul Wright
    0

    Just getting error: The type or namespace name 'IPublsihedElement' could not be found (are you missing a using directive or an assembly reference?)

    This is what I've put in to get that error

        @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
    @if (Model.HasValue("parts")){
    
            var items = Model.Value<IEnumerable<IPublishedElement>>("parts");
            foreach(var item in items)
            {
                <h3>@item.GetProperty("title").Value()</h3>
               <div>     
                @{
                            var secondItems = item.Value<IEnumerable<IPublsihedElement>>("questions");
                            if(secondItems.Any()){
                            foreach(var secondItem in secondItems){
                                <h3>@secondItem.GetProperty("sectionNumber").Value()</h3>
                              }
                            }
                }
              </div>  
            }
    
    }
    
  • Amir Khan 1284 posts 2741 karma points
    Aug 21, 2020 @ 18:26
    Amir Khan
    0

    I've always used this for nested content as well:

    @using Umbraco.Web.Models
    
  • Micha Somers 134 posts 597 karma points
    Aug 25, 2020 @ 15:18
    Micha Somers
    0

    The specific error is due to a typo:

    Use IPublishedElement instead of IPublsihedElement

  • Paul Wright 16 posts 97 karma points
    Aug 23, 2020 @ 23:32
    Paul Wright
    0

    Adding that doesnt seem to make any difference

  • Paul Wright 16 posts 97 karma points
    Aug 23, 2020 @ 23:42
    Paul Wright
    0

    infact it now working, seem to be issues when putting the code in via partial views.

    remove the view and added directly to the template and showing fine now.

    thanks everyone for the help

Please Sign in or register to post replies

Write your reply to:

Draft