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
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....
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.
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.
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.
Below you see one of the models that throws the exception where JobOverview is a umbraco modelsbuilder generated model
This model is used by a view that has the following inherrit
The masterylayout the above view uses has the following inherrit
and finally below is the custom controller for the page that uses the custom model
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 :).
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
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.
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!
is working on a reply...