2017-08-24 100 views
0

我正在创建一个mvc 4应用程序,并且陷入了一个奇怪的问题。我经过一段时间后反复出现错误。System.MissingMethodException:在mvc4中为此对象定义的没有无参数构造函数

2017-08-23 17:18:19,985 [7]错误 - System.MissingMethodException:没有为此对象定义的无参数构造函数。 在System.RuntimeTypeHandle.CreateInstance(RuntimeType类型,布尔publicOnly,布尔NOCHECK,布尔& canBeCached,RuntimeMethodHandleInternal &构造函数,布尔& bNeedSecurityCheck) 在System.RuntimeType.CreateInstanceSlow(布尔publicOnly,布尔skipCheckThis,布尔fillCache,StackCrawlMark & stackMark) 在System.Activator.CreateInstance(类型类型,布尔非公开) 在System.Activator.CreateInstance(类型类型) 在System.Web.Mvc.DefaultModelBinder.BindComplexModel(controllerContext controllerContext,ModelBindingContext的BindingContext) 在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的,对象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; } 
} 
+0

“UserDTO”的模型是什么 - 是否包含无参数构造函数? (同样为'CustomersDTO'和'NotificationDTO'和'JobDTO') –

+0

这个声明看起来很可疑:'public UserDTO userdetail {get;组; }'。模型类是否有一个用于绑定的空参数构造器? –

+0

@StephenMuecke先生UserDTO,CustomersDTO,NotificationDTO和JobDTO是我共享层中的类。我在控制器中创建这些类的对象,然后将数据传递给数据访问层。 –

回答

0

只是要在这里预感。根据堆栈跟踪,存在模型绑定器不喜欢的问题。你的DTO类是否有无参数的构造函数?

我想调试这种奇怪的问题的方式是通过评论大部分模型,直到找到可用的一个版本。然后逐渐引入新的属性。这并不快,但至少你可以缩小范围并找到罪魁祸首。

+0

不,我的DTO类没有无参数的构造函数。 –

+0

正如我所说的,尝试从您的模型中评论一些东西,以便您可以确定哪些属性会生成错误。如果存在产生该错误的特定请求,您可能还想尝试缩小范围。 –

相关问题