Copied to clipboard

Flag this post as spam?

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


  • Dmitriy 168 posts 588 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 first, 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 164 posts 508 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 168 posts 588 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) 277 posts 704 karma points
    May 19, 2017 @ 18:09
    Paul Wright (suedeapple)
    101
     @{
    
           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 168 posts 588 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

  • Søren Kottal 702 posts 4497 karma points MVP 5x c-trib
    May 19, 2017 @ 19:56
    Søren Kottal
    4

    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 168 posts 588 karma points
    May 22, 2017 @ 08:36
    Dmitriy
    0

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

  • Richard Hamilton 79 posts 169 karma points
    Jul 11, 2017 @ 11:29
    Richard Hamilton
    1
    @{
    var items = Model.Content.ContentModules;
    
    
    foreach (var ncItem in items)
    {
        switch (ncItem.DocumentTypeAlias)
        {
            case "CM_basicTextModule":
                var basicTextModule = new Cm_basicTextModule(ncItem);
                @Html.Partial("CM_basicTextModule", basicTextModule);
                break;
    
            case "CM_halfBoxes":
                var halfBoxes = new Cm_halfBoxes(ncItem);
                @Html.Partial("CM_halfBoxes", halfBoxes);
                break;
    
            case "CM_fullWidthBordered":
                var fullWidthBordered = new Cm_fullWidthBordered(ncItem);
                @Html.Partial("CM_fullWidthBordered", fullWidthBordered);
                break;
        }
    }
    

    so I ended up doing something like this. Then I used a normal view to render out each one the way I needed. I'm sure I can tidy it up as I go.

  • Paul Wright (suedeapple) 277 posts 704 karma points
    Jul 11, 2017 @ 11:34
    Paul Wright (suedeapple)
    0

    Looks good to me

    You could probably refactor it a bit more, as the var declaration is a bit superfluous

     @Html.Partial("CM_basicTextModule", new Cm_basicTextModule(ncItem));
    
  • Richard Hamilton 79 posts 169 karma points
    Jul 11, 2017 @ 14:26
    Richard Hamilton
    0

    Yeah, you're right. Still a work in progress :)

  • Paul Wright (suedeapple) 277 posts 704 karma points
    Jul 11, 2017 @ 11:41
    Paul Wright (suedeapple)
    1
      @foreach (var module in Model.NcModules.OrderBy(x => x.SortOrder))
                {
    
                    switch (module.DocumentTypeAlias)
                    {
    
                        case "moduleTitle":
                            @Html.Partial("Modules/Title", new ModuleTitle(module))
                            break;
    
                        case "moduleText":
                            @Html.Partial("Modules/Text", new ModuleText(module))
                            break;
    
                        case "moduleTextAndImage":
                            @Html.Partial("Modules/TextAndImage", new ModuleTextAndImage(module))
                            break;
    
                        case "moduleImage":
                            @Html.Partial("Modules/Image", new ModuleImage(module))
                            break;
    
                        case "moduleImageGallery":
                            @Html.Partial("Modules/ImageGallery", new ModuleImageGallery(module))
                            break;
    
                        case "moduleReviewPicker":
                            @Html.Partial("Modules/ReviewPicker", new ModuleReviewPicker(module))
                            break;
    
                        case "moduleQuotePicker":
                            @Html.Partial("Modules/QuotePicker", new ModuleQuotePicker(module))
                            break;
    
                        case "moduleVideo":
                            @Html.Partial("Modules/Video", new ModuleVideo(module))
                            break;
    
                        case "moduleShare":
                            @Html.Partial("Modules/Share")
                            break;
    
                        case "moduleProjectPicker":
                            @Html.Partial("Modules/ProjectPicker", new ModuleProjectPicker(module))
                            break;
    
                        case "moduleButton":
                            @Html.Partial("Modules/Button", new ModuleButton(module))
                            break;
    
                        case "modulePagePicker":
                            @Html.Partial("Modules/PagePicker", new ModulePagePicker(module))
                            break;
    
                        case "moduleSubPages":
                            @Html.Partial("Modules/SubPages")
                            break;
    
                        case "moduleSiblings":
                            @Html.Partial("Modules/Siblings")
                            break;
    
                        default:
                        break;
                    }
    
                }
    
  • Dmitriy 168 posts 588 karma points
    Nov 02, 2017 @ 08:34
    Dmitriy
    0

    UPDATE: Now I using LINQ extension method .Cast<Type>()

    var result = Model.YourNestedContent.Cast<YourType>();
    

    I dont know is it a good idea on performance, but it works and code looks good.

    What do you think?

  • Paul Wright (suedeapple) 277 posts 704 karma points
    Nov 02, 2017 @ 10:54
    Paul Wright (suedeapple)
    1

    Depends if you can guarantee that all your NestedContent nodes are of a single Model type, and not null.

    As with a Cadburys creme egg - there's plenty of different ways/methods to eat it :-)

  • Dmitriy 168 posts 588 karma points
    Nov 03, 2017 @ 10:16
    Dmitriy
    0

    Good remark, Paul! Thanks.

    But I did't encounter with cases when need more then one type in nested content.

    Could you give an example?

  • Stuart 11 posts 34 karma points
    Dec 01, 2017 @ 15:15
    Stuart
    1

    Here is a little snippet that might help Dimitri

    @foreach (var cp in Model.NestedContent)
    {
        switch (cp.DocumentTypeAlias)
        {
            case FeaturedTopicPanel.ModelTypeAlias:
                @Html.Partial(cp.DocumentTypeAlias, new FeaturedTopicPanel(cp));
                break;
            case MultipleArticlePanel.ModelTypeAlias:
                @Html.Partial(cp.DocumentTypeAlias, new MultipleArticlePanel(cp));
                break;
            case PodPanel.ModelTypeAlias:
                @Html.Partial(cp.DocumentTypeAlias, new PodPanel(cp));
                break;
            case SingleVideoPanel.ModelTypeAlias:
                @Html.Partial(cp.DocumentTypeAlias, new SingleVideoPanel(cp));
                            break;
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft