【发布时间】:2011-07-19 13:11:37
【问题描述】:
免责声明:是的,我已经阅读了 googled 并阅读了前六名左右的热门歌曲,searched on SO 和 found lots of other posts,但他们都没有帮助我解决了这个问题...This one,尤其是this answer,似乎非常接近,但我仍然无法弄清楚。因此,尽管我似乎问了一个以前被问过很多次的问题,但请多多包涵。
问题
当我提交表单时,我收到一个System.MissingMethodException,说“没有为此对象定义无参数构造函数”。这似乎有许多不同的,相当常见的原因 - 两个最突出的原因是控制器缺少默认构造函数并且当 DI 框架尝试解决它时没有满足它的依赖关系,分别是输入模型在 action 方法上没有默认构造函数。从我的堆栈跟踪中,我推断后者似乎是我的问题 - 但是,我无处可去试图解决这个问题。
背景资料
我的堆栈跟踪:
[MissingMethodException: 没有为此对象定义无参数构造函数。]
System.RuntimeTypeHandle.CreateInstance(RuntimeType 类型, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +117
System.RuntimeType.CreateInstanceDefaultCtor(布尔publicOnly,布尔skipVisibilityChecks,布尔skipCheckThis,布尔fillCache)+247
System.Activator.CreateInstance(类型类型,布尔非公共)+106
System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +243
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +151
等等......它会持续一段时间......
但是,我的模型确实有一个无参数的构造函数(见下面的代码)!
我的 POST 动作方法签名:
[HttpPost]
public ActionResult AddObject(InspectionObjectEditModel input, IUser user)
// IUser is passed with a custom model binder, that is verified to work
我的 GET 操作方法,显示表单:
public ActionResult CreateObject()
{
var model = GetEditModelForID(0); // Calls new InspectionObjectEditModel()
model.PostAction = "AddObject";
return View(model);
}
InspectionObjectEditModel:
public class InspectionObjectEditModel : ViewModel<InspectionObject, int>, IInspectionObjectData
{
// ReSharper warns on the constructor, because it's redundant
public InspectionObjectEditModel() { }
#region Properties for editing
[Required]
[DisplayNameLocalized("Littera")]
public virtual string Littera { get; set; }
[Required]
[DisplayNameLocalized("IInspectionObjectData_Type")]
public virtual InspectionObjectType Type { get; set; }
[Required, NotNull]
[DisplayNameLocalized("IInspectionObjectData_Name")]
public virtual string Name { get; set; }
[Required]
[DisplayNameLocalized("IInspectionObjectData_Owner")]
public virtual string Owner { get; set; }
[Required]
[DisplayNameLocalized("IInspectionObjectData_Address")]
public virtual string Address { get; set; }
[Required]
[DisplayNameLocalized("IInspectionObjectData_Caretaker")]
public virtual string Caretaker { get; set; }
[DisplayNameLocalized("IInspectionObjectData_Remarks")]
public virtual string Remarks { get; set; }
[Required]
[DisplayNameLocalized("IInspectionObjectData_X")]
public virtual float PlacementX { get; set; }
[Required]
[DisplayNameLocalized("IInspectionObjectData_Y")]
public virtual float PlacementY { get; set; }
[Required]
[DisplayNameLocalized("IInspectionObjectData_Z")]
public virtual float PlacementZ { get; set; }
#endregion
#region Data for form elements
public virtual List<InspectionObjectType> Types { get; set; }
public virtual bool Geocode { get; set; }
//public Expression<Action<InspectionController>> PostAction { get; set; }
public virtual string PostAction { get; set; }
#endregion
#region Properties that won't be edited
public virtual Project Project { get; set; }
public virtual DateTime Created { get; set; }
public virtual User CreatedByUser { get; set; }
public virtual DateTime? LastUpdated { get; set; }
public virtual User LastUpdatedByUser { get; set; }
public IList<InspectionActivity> Activities { get; set; }
#endregion
}
【问题讨论】: