Copied to clipboard

Flag this post as spam?

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


  • Dmitriy 10 posts 100 karma points
    May 19, 2017 @ 15:03
    Dmitriy
    0

    How to return strongly typed object from Nested Content?

    Using the Nested Content.

    I wanted to return a list of my nested document types objects, but dont know how.

    So, at firts, I tried to just use my Nested property, but it return a IEnumerable<IPublishedContent> instead list of my strongly typed nested objects. It does't let me use direct syntax like item.MyProperty.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<HomePage>
     @{
            // does'not works
           var items = Model.Content.MyNestedItems;
           foreach (var item in items)
           {                          
              @item.MyProperty // Not found coz it a IPublishedContent
            }                      
       }
    

    How to do it propely?

  • Craig Mayers 79 posts 265 karma points
    May 19, 2017 @ 15:35
    Craig Mayers
    1

    Hi Dmitriy,

    Can you try the following:

          @item.GetPropertyValue("MyPropertyName")
    

    OR you could try:

              @item.AsDynamic().MyPropertyName
    

    Let me know how it goes.

    Regards

    Craig

  • Dmitriy 10 posts 100 karma points
    May 19, 2017 @ 17:01
    Dmitriy
    0

    Thanks, Craig. It helped, but it still doesn't let use a direct syntax.

    Do an other options exist?

    May be a library or other approach?

  • Paul Wright (suedeapple) 155 posts 424 karma points
    May 19, 2017 @ 18:09
    Paul Wright (suedeapple)
    100
     @{
    
           var ncItems = Model.Content.MyNestedItems;
    
           foreach (var ncItem in ncItems)
           {                          
              // Article - is MY model
              // You might want to do a switch/if statement to assign the nc Item to the right Model
              // A way to determine the type of ncItem is by it's DTA
              // string nodeAlias = ncItem.DocumentTypeAlias;
    
            var item = new Article(ncItem);
    
            @item.MyProperty
    
            }                      
       }
    
  • Dmitriy 10 posts 100 karma points
    May 22, 2017 @ 08:07
    Dmitriy
    0

    Thank you, Paul. It is exactly what i wanted!

    Now, I have an idea to create a generic class to use it as a model instead UmbracoTemplatePage (and inherit that class).

  • Søren Kottal 227 posts 1155 karma points c-trib
    May 19, 2017 @ 19:56
    Søren Kottal
    1

    I usually do like

    Model.Content.NestedItems.Select(x => new Article(x))
    

    I think you could also do

    foreach (Article article in Model.Content.NestedArticles)
    

    But as Paul wrote, a switch for determining the model if there is more than one type in the nest, would be necessary.

  • Dmitriy 10 posts 100 karma points
    May 22, 2017 @ 08:36
    Dmitriy
    0

    Tnak you, Søren I will use it too.

Please Sign in or register to post replies

Write your reply to:

Draft