加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – MissingMethodException:“此对象没有无参数构

发布时间:2020-12-16 06:46:06 所属栏目:asp.Net 来源:网络整理
导读:免责声明:是的,我已经 googled并阅读了0700和 found lots的前六个左右,但是他们都没有帮助我解决这个问题… This one,特别是 this answer,似乎非常接近,但我仍然无法弄明白.因此,即使我似乎问了一个之前被问过很多次的问题,请耐心等待. 题 当我提交表单时,
免责声明:是的,我已经 googled并阅读了0700和 found lots的前六个左右,但是他们都没有帮助我解决这个问题… This one,特别是 this answer,似乎非常接近,但我仍然无法弄明白.因此,即使我似乎问了一个之前被问过很多次的问题,请耐心等待.

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

背景信息

我的堆栈跟踪:

[MissingMethodException: No parameterless constructor defined for this object.]

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(Boolean publicOnly,Boolean skipVisibilityChecks,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
etc… it goes on for a while…

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

我的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没有无参数构造函数,则可能导致该异常.您的模型类不仅需要无参数构造函数,我相信其公共属性的类型也可以.

编辑…

我认为你通过为你的模型使用这样一个复杂的类来增加你的问题机会.我一直希望保持我的视图模型尽可能简单 – 非常简单.然后,您可以使用自动映射器转移到更全功能的对象,或使用聚合,或者如果只有少数属性,只需在控制器操作方法中按属性执行属性.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读