asp.net-mvc – MissingMethodException:“此对象没有无参数构
免责声明:是的,我已经
googled并阅读了0700和
found
lots的前六个左右,但是他们都没有帮助我解决这个问题…
This one,特别是
this answer,似乎非常接近,但我仍然无法弄明白.因此,即使我似乎问了一个之前被问过很多次的问题,请耐心等待.
题 当我提交表单时,我得到一个System.MissingMethodException,说“没有为此对象定义无参数构造函数”.似乎有许多不同的,相当常见的原因 – 两个最突出的原因是Controller缺少默认构造函数,并且在DI框架试图解析它时没有满足它的依赖性,分别表示输入模型在action方法上没有默认的构造函数.从我的堆栈跟踪中,我推断出后者似乎是我的问题 – 然而,我无处可去尝试解决这个问题. 背景信息 我的堆栈跟踪:
但是,我的模型确实有一个无参数构造函数(参见下面的代码)! 我的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 } 解决方法
如果InspectionObjectType没有无参数构造函数,则可能导致该异常.您的模型类不仅需要无参数构造函数,我相信其公共属性的类型也可以.
编辑… 我认为你通过为你的模型使用这样一个复杂的类来增加你的问题机会.我一直希望保持我的视图模型尽可能简单 – 非常简单.然后,您可以使用自动映射器转移到更全功能的对象,或使用聚合,或者如果只有少数属性,只需在控制器操作方法中按属性执行属性. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – .net Mvc 3 Ajax.BeginForm,获取表单元素
- C#_Excel数据读取与写入_自定义解析封装类_支持设置标题行位
- asp.net-mvc-4 – 如何获取mvc中的用户IP地址
- asp.net – 每5秒自动刷新一次Updatepanel
- 获取当前的ASP.NET机器密钥
- entity-framework – DbContext未在ASP.Net MVC中使用SQL S
- asp.net – 如何为WCF服务创建重定向?
- asp.net-mvc – 存在多个获取操作时的MVC API路由
- asp.net-mvc – ModelState.AddModelError编码HTML
- ASP.NET如何访问公共属性?