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 445 posts 1059 karma points
    Mar 29, 2019 @ 13:28
    Ayo Adesina
    0

    Model Builder - How to covert IPublishedContent in to strongly typed Model

    Hi Guys,

    I'm using the out of the box Model Builder that comes with Umbraco I have enabled it in the web.config file like so:

    <add key="Umbraco.ModelsBuilder.Enable" value="true" />
    <add key="Umbraco.ModelsBuilder.ModelsMode" value="Dll" /> 
    

    Umbraco Is successfully creating the Model classes and I can see them in my visual studio project.

    In my controller, I'm using one of the generated models and I am getting all the values. For example:

         public ActionResult Index(Home model)
            {
                //model is now a strongly typed (Home Document type)
                var test = model.Descendants("myContentTypeAlias")
            }
    

    In the example above the variable test is a collection of IPublishedContent - My question is how can I make that list strongly typed based on the models that Umbraco has created.

    Thanks in advance.

  • Søren Kottal 713 posts 4571 karma points MVP 6x c-trib
    Mar 29, 2019 @ 13:31
    Søren Kottal
    102

    Hi Ayo

    You can add .OfType<MyContentAlias>() to model.Descendants("myContentTypeAlias") to get your collection in the right model.

  • Ayo Adesina 445 posts 1059 karma points
    Mar 29, 2019 @ 13:58
    Ayo Adesina
    0

    Thank you! - Exactly what I needed.... do you know where this is in the documentation, without your answer I would have struggled to find that.

    Thanks!

  • Marc-André 63 posts 279 karma points
    Sep 10, 2019 @ 17:39
    Marc-André
    0

    Thanks Soren!

    I got just a little issue with this, my query returns me 0 result.

    Is there anyway to specify a document type as a Custom Model or I am missing something?

    public class ProductModel : ContentModel
      {
        public ProductModel(IPublishedContent content) : base(content)
        {
    
        }
    }
    
    @{
    var allProducts = Model.Descendants("product").OfType<ProductModel>();
              foreach (ProductModel product in allProducts)
    }
    
  • Søren Kottal 713 posts 4571 karma points MVP 6x c-trib
    Sep 10, 2019 @ 20:17
    Søren Kottal
    0

    Hi Marc-Andre

    Is your ProductModel a model generated by ModelsBuilder? It has to be, else you can't cast it that way.

  • Marc-André 63 posts 279 karma points
    Sep 11, 2019 @ 12:27
    Marc-André
    0

    Yes it is!

    Now I got the error "Cannot bind ProductModel to ContentModel"

  • Marc-André 63 posts 279 karma points
    Sep 11, 2019 @ 13:18
    Marc-André
    0

    Ok got it. I had to pass my IPublishedContent as an agrument -_- hehe

  • Jeroen Breuer 4909 posts 12266 karma points MVP 5x admin c-trib
    Sep 11, 2019 @ 13:36
    Jeroen Breuer
    0

    Hi,

    You could probably also try the following:

    model.Descendants<MyContentAlias>("myContentTypeAlias")
    

    Jeroen

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies