2012-06-28 170 views
9

我正在創建一個MVC4應用程序,當我嘗試從我的視圖中找回表單到我的控制器時遇到了一些麻煩。System.MissingMethodException:爲此對象定義的無參數構造函數。 MVC4

我讀了幾篇關於主題的帖子,但不幸的是,我沒有設法找到哪個對象沒有無參數的構造函數。我所有的模型類都有一個沒有參數的構造函數。而在調試中,錯誤堆棧只是在Internet Explorer中,但在Visual Studio中沒有任何事情發生。

這是錯誤堆棧:

異常詳細信息:system.missingMethodException而:此對象定義無參數的構造函數。

源錯誤:

在當前web請求的執行過程中生成未處理的異常。關於異常的來源和位置的信息可以使用下面的異常堆棧跟蹤來標識。

堆棧跟蹤:

[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 
    System.Web.Mvc.DefaultModelBinder.UpdateCollection(ControllerContext controllerContext, ModelBindingContext bindingContext, Type elementType) +545 
    System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +609 
    System.Web.Mvc.DefaultModelBinder.GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder) +33 
    System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) +497 
    System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) +283 
    System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +677 
    System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +489 
    System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +153 
    System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) +883059 
    System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +137 
    System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +167 
    System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag) +27 
    System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +50 
    System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +137 
    System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +167 
    System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +869289 
    System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +137 
    System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +167 
    System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +27 
    System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +391 
    System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__3(AsyncCallback asyncCallback, Object asyncState) +827094 
    System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +137 
    System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +167 
    System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +27 
    System.Web.Mvc.<>c__DisplayClass6.<BeginProcessRequest>b__2() +283 
    System.Web.Mvc.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a() +19 
    System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func`1 func) +161 
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +405 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +375 

我的模型類:

public class Form 
    { 
     public Form() 
     { 
      this.Rows = new List<Row>(); 
     } 

     public List<Row> Rows { get; set; } 
    } 

    public class Row 
    { 
     protected Row() 
     { 
      this.Label = string.Empty; 
      this.Type = string.Empty; 
     } 

     public string Label { get; set; } 

     public string Type { get; set; } 
    } 

    public class SimpleRow : Row 
    { 
     public SimpleRow() 
     { 
      this.Value = string.Empty; 
     } 

     public string Value { get; set; } 
    } 


    public class DropDownRow : Row 
    { 
     public DropDownRow() 
     { 
      this.Content = new List<ContentDropDown>(); 
     } 

     public List<ContentDropDown> Content { get; set; } 
    } 


    public class ContentDropDown 
    { 
     public ContentDropDown() 
     { 
      this.Title = string.Empty; 
      this.Selected = false; 
     } 

     public string Title { get; set; } 

     public bool Selected { get; set; } 
    } 

回答

9

默認構造函數對你類是protected。嘗試將其更改爲public

+0

哦!非常感謝你,我確信我會在幾天之內看到它! – PuK

11

如果您的基礎類被定義爲接受參數,也會發生這種情況。由於MVC無法傳遞正確的值,因此失敗。嘗試聲明一個無參數的類。這解決了我在按鈕單擊回發期間的問題。

如:

public class Employee 
{ 

    public int EmployeeID {get;set;} 
    public string EmployeeName {get;set;} 
    public int CostCenter {get;set;} 
    public DateTime StartDate {get;set;} 

    public Employee(int employeeID) 
    { 
     //Initialize values 
    } 

    //Also include a parameter-less construct like below 
    public Employee() 
    { 
    } 


} 
相關問題