繁体   English   中英

获取“未为此对象定义无参数的构造函数。”发布视图模型时出错

[英]Getting “No parameterless constructor defined for this object.” Error while posting a viewmodel

我有一个称为StaffPerformanceVM的视图模型,其中使用Unity依赖关系注入器注入IPLLDetailService服务。 在使用此视图模型发布AddNew Staff Performance表单之前,一切似乎都正常。 我在最后得到错误。 我是依赖注入的新手,所以我不知道该如何解决?

这是我的视图模型的构造函数

    IPLLDetailService pLLDetailService;

    public StaffPerformanceVM(IPLLDetailService pLLDetailService)
    {
        this.pLLDetailService = pLLDetailService;
    }

这是我的Unity Bootstrapper类。

   private static IUnityContainer BuildUnityContainer()
    {
        var container = new UnityContainer();
        container.RegisterType(typeof(IRepository<>), typeof(Repository<>), new InjectionConstructor(new DbContext("PLLEntities")));

        container.RegisterType<IErrorEngine, ErrorEngine>();
        container.RegisterType<ILoggingEngine, LoggingEngine>();
        container.RegisterType<IIdentityManager, IdentityManager>();

        container.RegisterType<IEmailNotificationEngine, EmailNotificationEngine>();

        container.RegisterType<ILogService, LogService>(new ContainerControlledLifetimeManager());
        container.RegisterType<IPLLDetailService, PLLDetailService>(new ContainerControlledLifetimeManager());
        container.RegisterType<IObjectiveService, ObjectiveService>(new ContainerControlledLifetimeManager());
        container.RegisterType<ICheckInService, CheckInService>(new ContainerControlledLifetimeManager());
        container.RegisterType<IPersonService, PersonService>(new ContainerControlledLifetimeManager());

        container.RegisterType<StaffPerformanceVM>(new HierarchicalLifetimeManager());

        return container;
    }

这是我遇到的错误

没有为此对象定义无参数构造函数。

说明:执行当前Web请求期间发生未处理的异常。 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

异常详细信息:System.MissingMethodException:没有为此对象定义无参数构造函数。

源错误:

当前Web请求的执行期间生成了未处理的异常。 可以使用下面的异常堆栈跟踪来标识有关异常的来源和位置的信息。

- - - - -堆栈跟踪 - - - - - - - - -

[MissingMethodException:没有为此对象定义无参数的构造函数。] System.RuntimeTypeHandle.CreateInstance(RuntimeType类型,布尔publicOnly,布尔noCheck,布尔&canBeCached,RuntimeMethodHandleInternal&ctor,布尔&bNeedSecurityCheck)+0 System.RuntimeType.CreateInstanceSlow(布尔publicOnly,布尔skipCheckThis,布尔fillCache,StackCrawlMark和stackMark)+119 System.RuntimeType.CreateInstanceDefaultCtor(布尔publicOnly,布尔skipCheckThis,布尔fillCache,StackCrawlMark和stackMark)+247 System.Activator.CreateInstance(类型类型,布尔非公共)+83 System.Activator.CreateInstance(类型类型) )+11 System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext,ModelBindingContext bindingContext,Type modelType)+197

[MissingMethodException:没有为此对象定义无参数的构造函数。 对象类型'PerformanceLearningLog.ViewModels.StaffPerformanceVM'。] System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext,ModelBindingContext bindingContext,type modelType)+233 System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext,ModelBindingContext bindingContext)+530 System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext)+330 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext,ParameterDescriptor parameterDescriptor)+338 System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext,ActionDescriptor actionDescriptor)+105 System.Web.Mvc.Async。<> c__DisplayClass21.b__19(AsyncCallback asyncCallback,Object asyncState)+743 System.Web.Mvc.Async.WrappedAsyncResult 1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +14 System.Web.Mvc.Async.WrappedAsyncResultBase 1.Begin(Asy ncCallback回调,对象状态,Int32超时)+128 System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext,字符串actionName,AsyncCallback回调,对象状态)+343 System.Web.Mvc.Controller.b__1c(AsyncCallback asyncCallback,对象asyncState,ExecuteCoreState innerState)+25 System.Web.Mvc.Async.WrappedAsyncVoid 1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30 System.Web.Mvc.Async.WrappedAsyncResultBase 1.Begin(AsyncCallback回调,对象状态,Int32超时) )+128 System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback回调,对象状态)+465 System.Web.Mvc.Controller.b__14(AsyncCallback asyncCallback,对象callbackState,Controller控制器)+18 System.Web.Mvc.Async。 WrappedAsyncVoid 1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +20 System.Web.Mvc.Async.WrappedAsyncResultBase 1.Begin(AsyncCallback回调,对象状态,Int32超时)+128 System.Web.Mvc.Controller.BeginEx ecute(RequestContext requestContext,AsyncCallback回调,对象状态)+374 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext,AsyncCallback回调,对象状态)+16 System.Web.Mvc。 MvcHandler.b__4(AsyncCallback asyncCallback,对象asyncState,ProcessRequestState innerState)+52 System.Web.Mvc.Async.WrappedAsyncVoid 1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30 System.Web.Mvc.Async.WrappedAsyncResultBase 1.Begin(AsyncCallback回调,对象状态,Int32超时)+128 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,AsyncCallback回调,对象状态)+384 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext,AsyncCallback回调,对象状态) +48 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext上下文,AsyncCallback cb,对象extraData)+16 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecut ionStep.Execute()+103 System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值且已完成同步)+155

这是控制器代码

    [HttpPost]
    public async Task<ActionResult> AddPLLDetails(StaffPerformanceVM model)
    {
        String user = GetEmpNo();

        try
        {
            if (user != "")
            {
                LogCall(user, "PLL StaffPerformanceLog/AddPLLDetails-Post Page");
                model.StartDate = new DateTime(model.StartDateYear, model.StartDateMonth, model.StartDateDay);
                model.EndDate = new DateTime(model.EndDateYear, model.EndDateMonth, model.EndDateDay);
                model.AddPLLDetail(model);
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
        catch (Exception ex)
        {
            errorEngine.LogError(ex, "StaffPerformanceLog - AddPLLDetails-Post page not returning", user);
            throw ex;
        }
    }

当且仅当未提供其他构造函数时,编译器才会提供自动的Paramterless构造函数。 定义参数化构造函数后,必须手动提供无参数构造函数。

抱怨什么都没有。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM