C# '中的服务器错误/';应用没有为此对象定义无参数构造函数

C# '中的服务器错误/';应用没有为此对象定义无参数构造函数,c#,asp.net-mvc,C#,Asp.net Mvc,调用堆栈显示以下内容: [MissingMethodException: No parameterless constructor defined for this object.] System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolea

调用堆栈显示以下内容:

[MissingMethodException: No parameterless constructor defined for this object.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache) +86
System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache) +230
System.Activator.CreateInstance(Type type, Boolean nonPublic) +67
System.Activator.CreateInstance(Type type) +6
System.Web.Mvc.DefaultModelBinder.CreateModel(ModelBindingContext bindingContext, Type modelType) +277
System.Web.Mvc.<>c__DisplayClass1.<BindModel>b__0() +98
System.Web.Mvc.ModelBindingContext.get_Model() +51
System.Web.Mvc.DefaultModelBinder.BindModelCore(ModelBindingContext bindingContext) +2600
System.Web.Mvc.DefaultModelBinder.BindModel(ModelBindingContext bindingContext) +1067
System.Web.Mvc.DefaultModelBinder.BindProperty(ModelBindingContext parentContext, Type propertyType, Func`1 propertyValueProvider, String propertyName) +208
System.Web.Mvc.DefaultModelBinder.BindModelCore(ModelBindingContext bindingContext) +1787
System.Web.Mvc.DefaultModelBinder.BindModel(ModelBindingContext bindingContext) +1067
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ParameterInfo parameterInfo) +355
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(MethodInfo methodInfo) +439
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +288
System.Web.Mvc.Controller.ExecuteCore() +180
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +96
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +36
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +377
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +71
System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +36
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
[MissingMethodException:没有为此对象定义无参数构造函数。]
System.RuntimeTypeHandle.CreateInstance(RuntimeType类型、Boolean publicOnly、Boolean noCheck、Boolean&canBeCached、RuntimeMethodHandle&ctor、Boolean&bNeedSecurityCheck)+0
System.RuntimeType.CreateInstanceSlow(布尔publicOnly,布尔fillCache)+86
System.RuntimeType.CreateInstanceImpl(布尔publicOnly、布尔skipVisibilityChecks、布尔fillCache)+230
System.Activator.CreateInstance(类型,布尔非公共)+67
System.Activator.CreateInstance(类型)+6
System.Web.Mvc.DefaultModelBinder.CreateModel(ModelBindingContext,类型modelType)+277
System.Web.Mvc.c__显示类1.b__0()+98
System.Web.Mvc.ModelBindingContext.get_Model()+51
System.Web.Mvc.DefaultModelBinder.BindModelCore(ModelBindingContext-bindingContext)+2600
System.Web.Mvc.DefaultModelBinder.BindModel(ModelBindingContext-bindingContext)+1067
System.Web.Mvc.DefaultModelBinder.BindProperty(ModelBindingContext父上下文,类型propertyType,Func`1 propertyValueProvider,字符串propertyName)+208
System.Web.Mvc.DefaultModelBinder.BindModelCore(ModelBindingContext-bindingContext)+1787
System.Web.Mvc.DefaultModelBinder.BindModel(ModelBindingContext-bindingContext)+1067
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ParameterInfo ParameterInfo)+355
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(MethodInfo MethodInfo)+439
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext ControllerContext,String actionName)+288
System.Web.Mvc.Controller.ExecuteCore()+180
System.Web.Mvc.ControllerBase.Execute(RequestContext-RequestContext)+96
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext RequestContext)+36
ProcessRequest(HttpContextBase httpContext)+377
ProcessRequest(HttpContext HttpContext)+71
System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext-HttpContext)+36
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+181
System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)+75
我有一个小表单,有一堆隐藏字段和一个提交按钮。 当我按下它时,我甚至从来没有按过要求的方法

我如何继续调试这个? 如果我知道什么对象没有无参数构造函数,那将是一个很好的开始。 这个物体在哪里?我怎样才能解决这个问题? 我知道这个问题很模糊,但目前我只知道这些

--编辑--
在我的表单中,我添加了Html.Hidden()输入。根据以前的操作,这些操作的值可以为“”。该操作使用ModelBinding。每当值为“”且数据类型为SelectList时,modelbinder就会在我身上运行

我对SelectList的工作方式感到越来越不舒服。。。
这个主意不错,但也有一些问题。

您是否删除了Default.aspx?要正确渲染根,需要将其放置到位。如果您试图使用依赖项注入,但容器设置不正确,也可能发生这种情况—这意味着没有ControllerFactory向DI容器请求实例。

要使用默认模型绑定器,操作参数中的自定义类必须具有无参数构造函数。否则,要么(1)创建自定义模型绑定器,要么(2)将基本类型而不是自定义类传递给操作。

我解决了由SelectList对象引起的问题,因为它不提供默认构造函数,所以我们不能使用in-out ViewModel

我的视图模型类中还有返回SelectList实例的属性。我用Bind属性修饰我的类以排除以下属性:


[Bind(Exclude=“Countries”)]
公共类MyViewModel
{

}

希望有帮助,
罗杰

我的问题的根源也是SelectList

我有这个:

<%: Html.DropDownListFor(m => Model.Destinations, Model.Destinations)%>
<%: Html.DropDownListFor(m => Model.Destination, Model.Destinations)%>
Model.Destinations,Model.Destinations)%%>
当我应该有这个的时候:

<%: Html.DropDownListFor(m => Model.Destinations, Model.Destinations)%>
<%: Html.DropDownListFor(m => Model.Destination, Model.Destinations)%>
Model.Destination,Model.Destinations)%%>

Destination是用户的选择,Destinations是可能值的列表。使用复数SelectList两次会导致问题。

对于搜索此异常的用户,这里有一种更通用的诊断方法:

我发现诊断此问题的唯一简单方法是使用自己的代码尽可能地覆盖MVC,使其接近异常。然后,当发生此异常时,您的代码将在Visual Studio内部中断,您可以从堆栈跟踪中读取导致问题的类型

这似乎是处理这个问题的一种可怕的方式,但它非常快速,而且非常一致

例如,如果此错误发生在MVC DefaultModelBinder中(您可以通过检查堆栈跟踪来了解),则使用以下代码替换DefaultModelBinder:

public class MyDefaultModelBinder : System.Web.Mvc.DefaultModelBinder
{
    protected override object CreateModel(System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext, Type modelType)
    {
        return base.CreateModel(controllerContext, bindingContext, modelType);
    }
}
并更新您的Global.asax.cs:

public class MvcApplication : System.Web.HttpApplication
{
...
    protected void Application_Start(object sender, EventArgs e)
    {
        ModelBinders.Binders.DefaultBinder = new MyDefaultModelBinder();
    }
}
现在,下一次出现该异常时,VisualStudio将停止在MyDefaultModelBinder类中,您可以检查“modelType”属性以查看是什么类型导致了该问题


上面的示例仅适用于在模型绑定期间出现“没有为该对象定义无参数构造函数”异常的情况。但MVC中的其他扩展点(例如控制器构造)也可以编写类似的代码。

您试图调用的操作的签名是什么?您是否向控制器的构造函数添加了参数或删除了控制器上的默认构造函数?Todd,这不是问题所在。看看这堆东西;控制器已创建。这是一个模型,可能是action.public ActionResult ProductMix的参数之一(SearchBag SearchBag,IngredientBag ingr