Imagine being paid to improve your favorite CMS!

It could be you! Umbraco HQ are hiring and are looking for both talents and senior developers. Wonderful workplace, friendly colleagues and competitive salary.

Copied to clipboard

Flag this post as spam?

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


  • Dmitriy 33 posts 173 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 134 posts 448 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 33 posts 173 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) 164 posts 436 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 33 posts 173 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 239 posts 1363 karma points c-trib
    May 19, 2017 @ 19:56
    Søren Kottal
    2

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

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

  • Richard 25 posts 96 karma points
    Jul 11, 2017 @ 11:29
    Richard
    0
    @{
    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) 164 posts 436 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 25 posts 96 karma points
    Jul 11, 2017 @ 14:26
    Richard
    0

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

  • Paul Wright (suedeapple) 164 posts 436 karma points
    Jul 11, 2017 @ 11:41
    Paul Wright (suedeapple)
    0
      @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;
                    }
    
                }
    
Please Sign in or register to post replies

Write your reply to:

Draft