Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MissingMethodException: "No parameterless constructor for this object"

Disclaimer: Yes, I've googled and read through the top six or so hits, searched on SO and found lots of other posts, but neither of them has helped me resolve this... This one, and particularly this answer, seems to come pretty close, but I still can't figure it out. Thus, please bear with me even though I seem to ask a question that has been asked many times before.

Question

When I submit a form, I get a System.MissingMethodException saying "No parameterless constructor defined for this object." There seems to be a number of different, fairly common, causes for this - the two most prominent being that the Controller lacks a default constructor and doesn't have it's dependencies satisified when the DI framework tries to resolve it, respectively that the input model on the action method doesn't have a default constructor. From my stack trace, I've deduced that the latter seems to be my problem - however, I've come nowhere trying to troubleshoot this.

Background info

My stack trace:

[MissingMethodException: No parameterless constructor defined for this object.]

System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +117
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +247
System.Activator.CreateInstance(Type type, Boolean nonPublic) +106
System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +243
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +151
etc... it goes on for a while...

However, my model does have a parameterless constructor (see code below)!

My POST action method signature:

[HttpPost]
public ActionResult AddObject(InspectionObjectEditModel input, IUser user)
// IUser is passed with a custom model binder, that is verified to work

My GET action method, that shows the form:

public ActionResult CreateObject()
{
    var model = GetEditModelForID(0); // Calls new InspectionObjectEditModel()
    model.PostAction = "AddObject";
    return View(model);
}

The InspectionObjectEditModel:

public class InspectionObjectEditModel : ViewModel<InspectionObject, int>, IInspectionObjectData
{
    // ReSharper warns on the constructor, because it's redundant
    public InspectionObjectEditModel() { }

    #region Properties for editing
    [Required]
    [DisplayNameLocalized("Littera")]
    public virtual string Littera { get; set; }
    [Required]
    [DisplayNameLocalized("IInspectionObjectData_Type")]
    public virtual InspectionObjectType Type { get; set; }
    [Required, NotNull]
    [DisplayNameLocalized("IInspectionObjectData_Name")]
    public virtual string Name { get; set; }
    [Required]
    [DisplayNameLocalized("IInspectionObjectData_Owner")]
    public virtual string Owner { get; set; }
    [Required]
    [DisplayNameLocalized("IInspectionObjectData_Address")]
    public virtual string Address { get; set; }
    [Required]
    [DisplayNameLocalized("IInspectionObjectData_Caretaker")]
    public virtual string Caretaker { get; set; }
    [DisplayNameLocalized("IInspectionObjectData_Remarks")]
    public virtual string Remarks { get; set; }
    [Required]
    [DisplayNameLocalized("IInspectionObjectData_X")]
    public virtual float PlacementX { get; set; }
    [Required]
    [DisplayNameLocalized("IInspectionObjectData_Y")]
    public virtual float PlacementY { get; set; }
    [Required]
    [DisplayNameLocalized("IInspectionObjectData_Z")]
    public virtual float PlacementZ { get; set; }
    #endregion

    #region Data for form elements
    public virtual List<InspectionObjectType> Types { get; set; }
    public virtual bool Geocode { get; set; }

    //public Expression<Action<InspectionController>> PostAction { get; set; }
    public virtual string PostAction { get; set; }
    #endregion

    #region Properties that won't be edited
    public virtual Project Project { get; set; }
    public virtual DateTime Created { get; set; }
    public virtual User CreatedByUser { get; set; }
    public virtual DateTime? LastUpdated { get; set; }
    public virtual User LastUpdatedByUser { get; set; }
    public IList<InspectionActivity> Activities { get; set; }
    #endregion
}
like image 885
Tomas Aschan Avatar asked Jul 19 '11 13:07

Tomas Aschan


2 Answers

If InspectionObjectType doesn't have a parameterless constructor it may cause that exception. Not only does your model class need a parameterless constructor, I believe the types of its public properties do as well.

edit...

I think you are increasing your chances of problems by using such a complex class for your model. I have always preferred to keep my view models as simple as possible - really simple. You can then use an auto mapper to transfer to a more full featured object, or use aggregation, or if there are only a few properties, just do it property by property in your controller action method.

like image 137
hatchet - done with SOverflow Avatar answered Oct 24 '22 05:10

hatchet - done with SOverflow


The problem is that some of the types used in this monster domain model don't have default constructors: InspectionActivity, User, Project, InspectionObjectType or some of the properties they are referencing, or some of the objects used in the base class ViewModel<InspectionObject, int>. Using view models really would make things simpler.

like image 2
Darin Dimitrov Avatar answered Oct 24 '22 03:10

Darin Dimitrov