Copied to clipboard

Flag this post as spam?

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


  • Joël 7 posts 98 karma points
    Apr 05, 2023 @ 08:23
    Joël
    0

    ModelBindingException on live environments after restarts/umbraco updates

    Hello,

    I have a project running that use custom controllers and custom view models and now and then after a server restart or umbraco update these custom models will throw a ModelBindingException as you can see below. I can't seem to find the problem that causes it. A server restart fixes the problem until it happens again.

    Umbraco.Web.Mvc.ModelBindingException: Cannot bind source content type projectname.Web.AppCode.Models.JobOverviewModel to model type projectname.Web.AppCode.Models.JobOverviewModel
    

    Below you see one of the models that throws the exception where JobOverview is a umbraco modelsbuilder generated model

    namespace projectname.Web.AppCode.Models
    {
        public class JobOverviewModel : JobOverview
        {
            public JobOverviewModel(IPublishedContent content) : base(content) { }
    
            public IEnumerable<OfferModel> Offers { get; set; } = new HashSet<OfferModel>();
        }
    }
    

    This model is used by a view that has the following inherrit

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<JobOverviewModel>
    

    The masterylayout the above view uses has the following inherrit

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    

    and finally below is the custom controller for the page that uses the custom model

    public class JobOverviewController : RenderMvcController
    {
        private readonly RecruiteeClient _recruiteeClient;
    
        public JobOverviewController(
            RecruiteeClient recruiteeClient
            )
        {
            _recruiteeClient = recruiteeClient;
        }
    
        public ActionResult JobOverview(ContentModel model)
        {
            var newModel = new JobOverviewModel(model.Content);
    
            var offers = _recruiteeClient.AllOffers();
    
            newModel.Offers = offers;
    
            return CurrentTemplate(newModel);
        }
    }
    

    As far as I know I have followed the documentation correctly but maybe I have missed something.

    I hope anyone here can help me with this :).

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Apr 25, 2023 @ 08:52
    Marc Goodson
    100

    Hi Joël

    Do you compile your solution with Visual Studio and then deploy to the server?

    or do you work entirely on the site on the production server?

    My only reason for asking is the namespace you have for your JobOverviewModel is AppCode

    Which makes me think it might be in the App_Code folder, which is a special folder in the DotNet Framework, whose contents are automatically compiled at runtime.

    so my guess would be the mismatch between the models might be something to do with the dynamic compilation of the model after a restart and if it's not quite ready yet or there is a previous dynamically compiled version knocking around in the temp folder.

    Anyway if you were using Visual Studio and compiling and deploying the project, it would be interesting to move that model outside of the App_Code folder so it is compiled explicitly, instead of on the fly - and see if that would remove the occurrence of the error....

    regards

    Marc

  • Joël 7 posts 98 karma points
    Apr 25, 2023 @ 13:13
    Joël
    0

    Hello Marc,

    Thanks for you reply. Your suggestion is actually something I haven't tested yet, so that's great! I have now moved these models to the Core project of the solution and the pages still seem to work locally.

    I have pushed these changes to production so now we have to wait a while and see if it worked.

    I will reply again with the results.

  • Joël 7 posts 98 karma points
    Jun 15, 2023 @ 05:49
    Joël
    0

    It seems the solution was to bring the models/controllers out of the App_Code folder and place them elsewhere. I have now put them in the Core project of the solution.

    Thanks for your help!

Please Sign in or register to post replies

Write your reply to:

Draft