callstack显示以下内容:
[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
Run Code Online (Sandbox Code Playgroud)
我有一个带有一堆隐藏字段和一个提交按钮的小表单.当我按它时,我甚至都没有按下要求的方法.
我该如何继续调试呢?如果我知道WHAT对象没有无参数构造函数,那将是一个很好的开始.这个对象在哪里?我怎么解决这个问题?我知道这个问题很模糊,但目前我只有这个......
--EDIT--
在我的表单中我添加了Html.Hidden()输入.根据以前的操作,这些操作的值可以为"".该操作使用了ModelBinding.每当值为""且数据类型为SelectList时,模型绑定器就会对我进行berzerk.
我对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);
}
}
Run Code Online (Sandbox Code Playgroud)
并更新您的Global.asax.cs:
public class MvcApplication : System.Web.HttpApplication
{
...
protected void Application_Start(object sender, EventArgs e)
{
ModelBinders.Binders.DefaultBinder = new MyDefaultModelBinder();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,下次出现异常时,Visual Studio将停止在MyDefaultModelBinder类中,您可以检查"modelType"属性以查看导致问题的类型.
上面的示例适用于仅在模型绑定期间获得"为此对象定义的无参数构造函数"异常时.但是可以为MVC中的其他扩展点编写类似的代码(例如,控制器构造).
小智 6
我的视图模型类中还有返回SelectList实例的属性.我使用Bind属性修饰了我的类,以便像这样排除这些属性:
[Bind(Exclude = "Countries")]
public class MyViewModel
{
...
public SelectList Countries { get; set; }
Run Code Online (Sandbox Code Playgroud)
}
[Bind(Exclude = "Countries")]
public class MyViewModel
{
...
public SelectList Countries { get; set; }
Run Code Online (Sandbox Code Playgroud)
}
[Bind(Exclude = "Countries")]
public class MyViewModel
{
...
public SelectList Countries { get; set; }
Run Code Online (Sandbox Code Playgroud)
}
希望有所帮助,罗杰
归档时间: |
|
查看次数: |
22322 次 |
最近记录: |