System.MissingMethodException:在mvc4中没有为此对象定义无参数构造函数

时间:2017-08-24 08:47:59

标签: c# .net asp.net-mvc-4

我正在创建一个mvc 4应用程序,我遇到了一个奇怪的问题。经过一段时间后,我反复听到错误。

  

2017-08-23 17:18:19,985 [7]错误 - System.MissingMethodException:没有为此对象定义无参数构造函数。          在System.RuntimeTypeHandle.CreateInstance(RuntimeType类型,Boolean publicOnly,Boolean noCheck,Boolean& canBeCached,RuntimeMethodHandleInternal& ctor,Boolean& bNeedSecurityCheck)          在System.RuntimeType.CreateInstanceSlow(Boolean publicOnly,Boolean skipCheckThis,Boolean fillCache,StackCrawlMark& stackMark)          在System.Activator.CreateInstance(Type type,Boolean nonPublic)          在System.Activator.CreateInstance(类型类型)          在System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext,ModelBindingContext bindingContext)          在System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext,ParameterDescriptor parameterDescriptor)          在System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext,ActionDescriptor actionDescriptor)          在System.Web.Mvc.Async.AsyncControllerActionInvoker。<> c__DisplayClass25.b__1e(AsyncCallback asyncCallback,Object asyncState)

我在我的模型类中使用可空类型以及选择列表。我在所有模型类中都有默认构造函数。我的模型类的样本是这样的:

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; }

}

我无法解决此错误,因为有时某个方法会抛出此错误,而其他时间则完全正常。

任何帮助都会受到赞赏,因为我很长时间都会受到这种打击。提前谢谢。

UserDTO类是这样的:

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; }
}

1 个答案:

答案 0 :(得分:0)

在这里预感。基于堆栈跟踪,模型绑定器不喜欢这个问题。您的DTO类是否具有无参数构造函数?

我调试此类奇怪问题的方法是通过注释掉大部分模型,直到找到一个有效的版本。然后逐步引入新的属性。它并不快,但至少你可以缩小范围并找到罪魁祸首。

相关问题