Copied to clipboard

Flag this post as spam?

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


  • JoskerVemeulen 55 posts 227 karma points
    May 03, 2019 @ 07:12
    JoskerVemeulen
    0

    Retreive Nested Contend

    Hi, I have a doctype with nestedcontent(html grids). And I have found this code on the forum(V7):

    @{
        var nestedContent = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("contentTabs");
    
        foreach (var item in nestedContent)
        {
            @Html.GetGridHtml(item.GetProperty("contentGrid"));
        }
    }
    

    How do I use this in V8? GetPropertyValue is not supported. My code now is:

     var nestedContent = Model.Value<IEnumerable<IPublishedContent>>("wYSIWYGGrid");
    
        foreach (var item in nestedContent)
            {
               @Html.GetGridHtml(item.GetProperty("wYSIWYGElement"));
    
        }
    

    But this returns a Object reference not set to an instance of an object.

    Anyone?

  • Connor Forsyth 7 posts 88 karma points
    May 03, 2019 @ 07:35
    Connor Forsyth
    0

    Hi,

    For nested content this is what I typically do:

    IEnumerable<IPublishedContent> nestedContent = Model.Value<IEnumerable<IPublishedContent>>("wYSIWYGGrid");
    

    Then as it could return empty or null, I wrap it round an if statement. Then after that, you can loop through and get the content you need.

    if(nestedContent != null && nestedContent.Any()){
    
    foreach(IPublishedContent item in nestedContent){
    
    @*In here you can now retrieve the content you need, e.g.*@
    
    @Html.GetGridHtml(item.Value("wYSIWYGElement"));
    }
    
    
    }
    
  • JoskerVemeulen 55 posts 227 karma points
    May 03, 2019 @ 11:42
    JoskerVemeulen
    100

    Thanks for this, this is my working code

    var nestedContent = Model.Value<IEnumerable<IPublishedElement>>("wYSIWYGGrid");
    
     if (nestedContent != null && nestedContent.Any()){
    
            foreach (var item in nestedContent)
            {
    
                @*In here you can now retrieve the content you need, e.g.*@
                @Html.GetGridHtml(item.GetProperty("wYSIWYGElement"));
            }
      }
    
  • k 242 posts 627 karma points
    Sep 22, 2019 @ 14:20
    k
    0

    Hello Joser,

    I have used same code as above on Umbraco 8. But I am getting the below error :

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Can you please help.

    Thanks,

    Kusum

Please Sign in or register to post replies

Write your reply to:

Draft