简体   繁体   中英

System.MissingMethodException: No parameterless constructor defined for this object in mvc4

I am creating a mvc 4 application and i am stuck in a strange problem. I am getting following error repeatedly after some interval of time.

2017-08-23 17:18:19,985 [7] ERROR - System.MissingMethodException: No parameterless constructor defined for this object. at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.Activator.CreateInstance(Type type) at System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.b__1e(AsyncCallback asyncCallback, Object asyncState)

I am using nullable types as well as selectlist in my model classes. i have default constructor in all model classes. Sample of my model class is like this:

public class BiddingFirstStepModel
{
    //--------------------------Default Constructor--------

    public BiddingFirstStepModel() { }

    //-----------------------------------------------------

    public string jobname { get; set; }
    public string jobtype { get; set; }
    public int jobtypeid { get; set; }
    public string jobreference { get; set; }
    public string customername { get; set; }
    public int? quantity { get; set; }
    public string department { get; set; }
    public int? departmentid { get; set; }
    public string description { get; set; }
    public DateTime? EndDate { get; set; }
    public string customerid { get; set; }
    public string rfqno { get; set; }
    public string productpartname { get; set; }
    public string productpartcode { get; set; }
    public string designlocation { get; set; }
    public DateTime? sopdate { get; set; }
    public int? lifecycle { get; set; }
    public string productapplication { get; set; }
    public string enduser { get; set; }
    public bool loa { get; set; }
    public string JobOrderType { get; set; }
    public string SeriesJobRefrence { get; set; }

    //--------NPI------------
    public string producthead { get; set; }
    public Int64? expectedincreasequantity { get; set; }
    public DateTime? initialsamples { get; set; }
    public Int64? tentativequantity { get; set; }
    public DateTime? ppap { get; set; }
    public string manufacturinglocation { get; set; }
    public string supplier { get; set; }

    //------------Customer Return------------------

    public Int64 quantitysupplied { get; set; }
    public string dmaicno { get; set; }

    //---------------------------------------------

    public HttpPostedFileBase excelbom { get; set; }

    public JobDTO jobdata = new JobDTO();
    public SelectList DepartmentList { get; set; }
    public SelectList JobTypeList { get; set; }
    public SelectList CustomersList { get; set; }
    public SelectList JobReferenceList { get; set; }
    public SelectList JobList { get; set; }
    public bool isedit = false;

    public bool IsRevise = false;

    public CustomersDTO[] CustomerData { get; set; }

    //-----------------------------------------------------

    public UserDTO userdetail { get; set; }

    //-----------------------------------------------------

    public string JobId { get; set; }

    //---------------------Notification---------------------

    public List<NotificationDTO> listofnotification { get; set; }

    //-------------------------------------------------------

    public JobDTO[] JobsArray { get; set; }

    //-------------------------------------------------------------------

    public List<CustomersDTO> CustomerDataList { get; set; }

}

I am not able to solve this error as sometimes a method throws this error while other time it works perfectly fine.

Any help will be appreciated as i'm struck in this for long. Thanks in advance.

UserDTO class is like this:

public class UserDTO : IUserDTO
{
    public Int64 Id { get; set; }
    public string UserId { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public int DepartmentId { get; set; }
    public string Email { get; set; }
    public int UserPrivilegeId { get; set; }
    public int LocationId { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime? ExpiredOn { get; set; }
    public byte[] Image { get; set; }
    public DateTime? DateOfBirth { get; set; }
    public string MobileNo { get; set; }
    public string DepartmentName { get; set; }
    public string UserPrivilege { get; set; }
    public string LocationName { get; set; }
    public string CreatedBy { get; set; }
    public string ImageName { get; set; }
    public string ImageExtension { get; set; }
    public Int64? ImageSize { get; set; }
    public string ImageSavePath { get; set; }
    public string ImageShowPath { get; set; }
    public string EditedBy { get; set; }
    public DateTime? EditedOn { get; set; }

    public string LocationShortName { get; set; }
}

Just going on a hunch here. Based on the stack trace there's an issue that the model binder does not like. Do your DTO classes have parameterless constructors?

The way I'd debug this type of weird issues is by commenting out most of the model until you find the one version that works. Then gradually introduce new properties. It's not quick, but at least you can narrow down and find the culprit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM