Is a public constructor on an object needed by ASP.NET MVC?

Is a public constructor on an object needed by ASP.NET MVC?

Old forum URL: forums.lhotka.net/forums/t/11891.aspx


TSF posted on Tuesday, March 19, 2013

I've always had the habit of defining a public constructor for my objects when being consumed by SL or WinRT, but I conditionally provide the private parameterless constructor for .NET projects. I'm consuming an object from an ASP.NET MVC project and I'm running into the "No parameterless constructor defined for this object" error.

If I turn the private constructor (for .NET compilation) into a public one, it solves the problem. Am I missing something on why a public one would be needed by ASP.NET? Thanks.

JonnyBee replied on Wednesday, March 20, 2013

Hi,

I believe the ASP.NET MVC DefaultModelBinder  expects all models to have a public default constructor.

The Csla.Web.Mvc.CslaModelBinder inherits from DefaultModelBinder and helps to suppress rule checking while setting properties and the call CheckRules when completed.

 

 

 

lazaroms replied on Monday, October 28, 2013

What if I need a parameter in my default public constructor?

JonnyBee replied on Tuesday, October 29, 2013

Hi,

Then you must also create your own model binder in order to add code that use the non-default constructor. You may look at the CslaModelBinder for a sample of this (see interface IModelCreator).

Please start new thread when you have a new question rather than posting new questions on a thread that is marked as answered.

Copyright (c) Marimer LLC