Copied to clipboard

Flag this post as spam?

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


  • Ayo Adesina 370 posts 926 karma points
    Jun 09, 2018 @ 18:09
    Ayo Adesina
    0

    Umbraco Model Binder - How to get strongly typed objects from child nodes

    I'm using Umbraco's Model Binder to generate strongly typed models from my document types to use in my code.

    This is working pretty well but I want to know how I can get strongly typed objects for the children of any given generated model.

    Here is an example:

      public ActionResult Index(HomePage model)
        {
            var components = model.Children.Where(x => x.DocumentTypeAlias == PageComponentsFolder.ModelTypeAlias)
                .Single().Children; 
        }
    

    HomePage is a strongly typed class generated by the Umbraco model builder. Under the home page node I have a page components folder with several other nodes that all inherit from a ComponentsBaseClass.

    How can I make my components variable above a strongly typed list of objects.

    Is this possible?

  • Sotiris Filippidis 246 posts 1217 karma points
    Jun 09, 2018 @ 18:22
    Sotiris Filippidis
    0

    I think you should drop the where and just use

    Children<T>()
    

    Note: If you are using composition, then you can request all pages that are composed with a specific page - those pages will implement an interface generated with the class, for example if you compose your pages with a document named BasePage and you need all children that are composed with BasePage you can have something like:

     .Children<IBasePage>()
    
  • Ayo Adesina 370 posts 926 karma points
    Jun 09, 2018 @ 18:45
    Ayo Adesina
    0

    I'm not using composition to create my document types (I should really look in to that, I have heard good things)

    But I tried to do this:

     var components = model.Children.Where(x => x.DocumentTypeAlias == PageComponentsFolder.ModelTypeAlias)
                    .Single().Children<ComponentRoot>();
    

    All my different component document types inherit from ComponentRoot, there are no Interfaces as such (or that I know about) when using the model binder.....

    I'm thinking I might have to just do it the old way, running out of time :-)

  • Ayo Adesina 370 posts 926 karma points
    Jun 11, 2018 @ 09:26
    Ayo Adesina
    100

    Ok this is what I ended up with in the end, here is an example of how to use strongly typed models generated by the Umbraco model binder.

    var components = model.Children.Where(x => x.DocumentTypeAlias == PageComponentsFolder.ModelTypeAlias)
                .Single().Children; 
    
        @foreach (var component in components)
        {    
        string componentNodeTypeAlias = component.DocumentTypeAlias;
    
            switch (componentNodeTypeAlias)
            {
                case SimpleHero.ModelTypeAlias:
                    @Html.Partial("component_simpleHero", component as SimpleHero)
                    break;
    
                case VideoWithHtml.ModelTypeAlias:
                    @Html.Partial("component_videoWithHTML", component as VideoWithHtml)
                    break;
            }
    
        }
    
  • Ben Norman 166 posts 272 karma points
    Jun 30, 2019 @ 07:00
    Ben Norman
    0

    Is there a way to get the class name from the ModelTypeAlias?

Please Sign in or register to post replies

Write your reply to:

Draft