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 430 posts 1023 karma points
    Jun 09, 2018 @ 08:27
    Ayo Adesina
    0

    How to use ModelBinder classes in Controllers when using Umbraco Cloud.

    First question... I have an Umbraco Cloud Project I have just started, and for the first time I want to use the ModelBinder.

    When using Umbraco Cloud you have a website instead of a Web Application.

    You also have a .Core Project where you have a controllers folder. How do I access the generated strongly typed classes in my controllers in my .Core project when the classes are generated in the Website project?

    Second question... Also, in the past before I started using the model binder, I would always make custom view models, do logic in my controller then pass that custom view model to my view.

    When using the Model Binder, assuming I can get the strong types classes in my controller, should I still be making a custom view model class now I have a strong typed object can I just pass that straight in to the view... I'm a little confused about the correct approach, my gut is telling me I should still using a custom view model.

    Thanks in advanced.

  • Ayo Adesina 430 posts 1023 karma points
    Jun 09, 2018 @ 14:26
    Ayo Adesina
    100

    OK, Looks like I have got it working but I'm not 100% sure if this is the way it should be done.

    Step 1.

    Sent Model Binder mode in the web config to DLL with the following settings

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

    This will cause Umbraco to generate model classes based on your Document Types. - (by clicking generate models button) which are located in:

    Umbraco.Web.PublishedContentModels.dll in the bin folder. of your .Web project

    Step 2

    From the .Core project Add a reference to Umbraco.Web.PublishedContentModels.dll the bin folder of the .Web project.

    Now in your controller classes in your .core project you can now use your strongly typed Generated model like this:

    public ActionResult Index(HomePage model)
    {
    
            var myvalue = model.SEotitle;  // Look strong typed 
    
            //more logic here
            return View(model);
    }
    

    Would love to know if this is how others are using model binder in cloud.

Please Sign in or register to post replies

Write your reply to:

Draft