2017-08-24 74 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

正如我所說的,嘗試從您的模型中評論一些東西,以便您可以確定哪些屬性會生成錯誤。如果存在產生該錯誤的特定請求,您可能還想嘗試縮小範圍。 –

相關問題