Posts

Showing posts with the label Invalidoperationexception

ASP.NET MVC: No Parameterless Constructor Defined For This Object

Answer : I just had a similar problem. The same exception occurs when a Model has no parameterless constructor. The call stack was figuring a method responsible for creating a new instance of a model. System.Web.Mvc.DefaultModelBinder. CreateModel (ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) Here is a sample: public class MyController : Controller { public ActionResult Action(MyModel model) { } } public class MyModel { public MyModel(IHelper helper) // MVC cannot call that { // ... } public MyModel() // MVC can call that { } } This can also be caused if your Model is using a SelectList, as this has no parameterless constructor : public class MyViewModel { public SelectList Contacts { get;set; } } You'll need to refactor your model to do it a different way if this is the cause. So using an IEnumerable<Contact> and writing an extension method that creates the ...