C# System.MissingMethodException: 没有为此对象定义无参数构造函数。MVC4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11246197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:06:38  来源:igfitidea点击:

System.MissingMethodException: No parameterless constructor defined for this object. MVC4

c#.netrazorasp.net-mvc-4

提问by PuK

I am creating a MVC4 application and I have got some troubles when I try to get back a form from my view to my controller.

我正在创建一个 MVC4 应用程序,当我尝试将表单从我的视图返回到我的控制器时遇到了一些麻烦。

I read several SO post on the topic but unfortunately, I did not managed to find which object does not have a parameterless constructor. All my model classes have a no parameter constructor. And in debug the error stack is just in Internet Explorer but nothing happen in visual studio.

我阅读了几篇关于该主题的 SO 帖子,但不幸的是,我没有找到哪个对象没有无参数构造函数。我所有的模型类都有一个无参数构造函数。在调试中,错误堆栈仅在 Internet Explorer 中,但在 Visual Studio 中什么也没有发生。

This is the error stack :

这是错误堆栈:

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

异常详细信息:System.MissingMethodException:没有为此对象定义无参数构造函数。

Source Error:

源错误:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常来源和位置的信息。

Stack Trace:

堆栈跟踪:

[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

My Model classes :

我的模型类:

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

采纳答案by ScottieMc

The default constructor on your Rowclass is protected. Try changing it to public.

你的Row类的默认构造函数是protected. 尝试将其更改为public.

回答by Bala SM

This can also happen if your underlying class is defined to accept parameter. Since MVC is unable to pass the right value it fails. Try declare a parameter-less class as well. This solved my problem during button click postback.

如果您的基础类被定义为接受参数,也会发生这种情况。由于 MVC 无法传递正确的值,因此它失败了。尝试声明一个无参数类。这解决了我在按钮单击回发期间的问题。

Eg:

例如:

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()
   {
   }


}