Copied to clipboard

Flag this post as spam?

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


  • Michaël Vanbrabandt 863 posts 3346 karma points c-trib
    May 23, 2019 @ 12:46
    Michaël Vanbrabandt
    0

    How to access Models from ModelsBuilder when developing packages?

    Hi all,

    I am working on a package which included document types, custom section, fetching external data, ...

    We have a DT called Products and a DT called Product Item which are added during the installation of the package.

    We have 2 properties that are hidden for everyone, because these are populated from the external datasource during the Render process in our RenderMvcController.

    public class ProductsController : RenderMvcController
    {
        // Index
        public override ActionResult Index(RenderModel model)
        {
            if (model?.Content != null && model.Content.Children.Any())
            {
                foreach (var productItem in model.Content.Children)
                {
                    // fill up the properties
                }
            }
    
            return base.Index(model);
        }
    }
    

    Inside this controller we have IPublishedContent objects which can't be changed so I need to convert them to my strongly typed models. But here I am stuck.

    My package solution has 3 projects: Core - Web - Site.

    Site contains the Umbraco installation, Views, ... So its basically my test environment. The Core contains the Core classes like repo, services, ... The Web contains my controllers, like the one above.

    How can I access my Strongly Typed models when using ModelsBuilder and make it flexible so that whatever mode you choose, my package can access the models? I need some kind of reference to the PublishedContentModels to access my package DT classes.

    /Michaël

  • Damiaan 440 posts 1295 karma points MVP 5x c-trib
    May 23, 2019 @ 14:08
    Damiaan
    0

    Use the LiveAppData for models builder and push the models to a different folder in your project. The Umbraco.ModelsBuilder.ModelsDirectory setting as documentated here: https://our.umbraco.com/Documentation/Reference/Templating/Modelsbuilder/Install-And-Configure is created for that.

    Like that you can include your models in the project which you want. (at least if you activate Umbraco.ModelsBuilder.AcceptUnsafeModelsDirectory)

  • Michaël Vanbrabandt 863 posts 3346 karma points c-trib
    May 24, 2019 @ 19:14
    Michaël Vanbrabandt
    0

    Hmmm now I see that the properties of the Models of the ModelsBuilder only have getters and no setters.

    How can I manipulate my typed model in my controller only for the View, so no update in the database?

    Reason: I have products, and I want to show the current price and stock info by making a call to the external tool which holds this data before showing it into the view.

    I can't use a ViewModel because then the editor doesn't have control about the stock and price properties in the backoffice. We have a flag which indicates to show the real price and stock info by making a call to the external tool, if false then it will use the current values as set in the backoffice.

    /Michaël

Please Sign in or register to post replies

Write your reply to:

Draft