MissingMethodException:“此对象没有无参数构造函数”

Tom*_*han 5 asp.net-mvc model-binding

免责声明:是的,我用Google搜索,并通过前六名左右的点击阅读,搜索了SO发现了 大量其他 职位,但他们都没有帮我解决这个...这其中,特别是这个答案,似乎非常接近,但我仍然无法弄清楚。因此,尽管我似乎问了一个以前被问过很多次的问题,但请耐心等待。

当我提交表单时,我得到一个System.MissingMethodException说法“没有为此对象定义无参数构造函数”。似乎有许多不同的,相当常见的原因 - 两个最突出的原因是控制器缺少默认构造函数并且在 DI 框架尝试解决它时没有满足它的依赖关系,分别是输入模型在 action 方法上没有默认构造函数。从我的堆栈跟踪中,我推断后者似乎是我的问题 - 但是,我没有尝试解决这个问题。

背景资料

我的堆栈跟踪:

[MissingMethodException: 没有为此对象定义无参数构造函数。]

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 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
等等......它持续了一段时间......

但是,我的模型确实有一个无参数构造函数(请参阅下面的代码)!

我的 POST 操作方法签名:

[HttpPost]
public ActionResult AddObject(InspectionObjectEditModel input, IUser user)
// IUser is passed with a custom model binder, that is verified to work
Run Code Online (Sandbox Code Playgroud)

我的 GET 操作方法,显示了表单:

public ActionResult CreateObject()
{
    var model = GetEditModelForID(0); // Calls new InspectionObjectEditModel()
    model.PostAction = "AddObject";
    return View(model);
}
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

hat*_*ica 4

如果 InspectionObjectType 没有无参数构造函数,则可能会导致该异常。您的模型类不仅需要无参数构造函数,我相信其公共属性的类型也需要无参数构造函数。

编辑...

我认为通过为模型使用如此复杂的类会增加出现问题的机会。我一直喜欢让我的视图模型尽可能简单——非常简单。然后,您可以使用自动映射器传输到功能更齐全的对象,或者使用聚合,或者如果只有几个属性,则只需在控制器操作方法中逐个属性地执行它。