简体   繁体   English

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

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

I have a view model called StaffPerformanceVM where I Inject IPLLDetailService service using Unity Dependency Injector. 我有一个称为StaffPerformanceVM的视图模型,其中使用Unity依赖关系注入器注入IPLLDetailService服务。 Everything seems working until I post the AddNew Staff Performance form using this view model. 在使用此视图模型发布AddNew Staff Performance表单之前,一切似乎都正常。 I am getting the error at the end. 我在最后得到错误。 I am new in Dependency Injection so I cannot figure it out how can I solve this? 我是依赖注入的新手,所以我不知道该如何解决?

Here is the Constructor of my View Model 这是我的视图模型的构造函数

    IPLLDetailService pLLDetailService;

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

Here is me Unity Bootstrapper class. 这是我的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;
    }

This is the error I am encountering 这是我遇到的错误

No parameterless constructor defined for this object. 没有为此对象定义无参数构造函数。

Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object. 异常详细信息:System.MissingMethodException:没有为此对象定义无参数构造函数。

Source Error: 源错误:

An unhandled exception was generated during the execution of the current web request. 当前Web请求的执行期间生成了未处理的异常。 Information regarding the origin and location of the exception can be identified using the exception stack trace below. 可以使用下面的异常堆栈跟踪来标识有关异常的来源和位置的信息。

---------Stack Trace----------------- - - - - -堆栈跟踪 - - - - - - - - -

[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, StackCrawlMark& stackMark) +119 System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +247 System.Activator.CreateInstance(Type type, Boolean nonPublic) +83 System.Activator.CreateInstance(Type type) +11 System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +197 [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: No parameterless constructor defined for this object. [MissingMethodException:没有为此对象定义无参数的构造函数。 Object type '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 对象类型'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 callback, Object state, Int32 timeout) +128 System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +343 System.Web.Mvc.Controller.b__1c(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState) +25 System.Web.Mvc.Async.WrappedAsyncVoid 1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30 System.Web.Mvc.Async.WrappedAsyncResultBase 1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +465 System.Web.Mvc.Controller.b__14(AsyncCallback asyncCallback, Object callbackState, Controller controller) +18 System.Web.Mvc.Async.WrappedAsyncVoid 1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +20 System.Web.Mvc.Async.WrappedAsyncResultBase 1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 System.Web.Mvc.Controller.BeginEx 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 callback, Object state) +374 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +16 System.Web.Mvc.MvcHandler.b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +52 System.Web.Mvc.Async.WrappedAsyncVoid 1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30 System.Web.Mvc.Async.WrappedAsyncResultBase 1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +384 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecut 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 step, Boolean& completedSynchronously) +155 ionStep.Execute()+103 System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值且已完成同步)+155

Here is the Controller Code 这是控制器代码

    [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;
        }
    }

The Compiler will provide a automatic Paramterless constructor if and only if there is no other Constructor given. 当且仅当未提供其他构造函数时,编译器才会提供自动的Paramterless构造函数。 Once you define a Parametised constructor, you have to provide the parameterless one manually. 定义参数化构造函数后,必须手动提供无参数构造函数。

It is complaning about there being none. 抱怨什么都没有。

暂无
暂无

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

相关问题 错误“未为此对象定义无参数的构造函数。” ViewModel中的SelectList错误 - Error “No parameterless constructor defined for this object.” with SelectList from ViewModel 没有为此对象定义无参数构造函数。 当我尝试使用 Unity 依赖时 - No parameterless constructor defined for this object. while i try for Unity Dependency CBO.FillCollection抛出“没有为此对象定义的无参数构造函数。”错误 - CBO.FillCollection throwing “No parameterless constructor defined for this object.” error Unity:没有为此对象定义无参数构造函数。 - Unity : No parameterless constructor defined for this object. 没有为此对象定义无参数构造函数。 MVC 5 - No parameterless constructor defined for this object. MVC 5 使用jQuery发布数据时,没有为此对象定义无参数构造函数 - No parameterless constructor defined for this object while posting data with jquery 错误:未为此对象定义无参数构造函数 - Error: No parameterless constructor defined for this object System.MissingMethodException:没有为此对象定义的无参数构造函数。 MVC4 - System.MissingMethodException: No parameterless constructor defined for this object. MVC4 “没有为此对象定义无参数的构造函数。”对于使用结构映射的控制器中的IBus - “No parameterless constructor defined for this object.” for IBus in controller using StructureMap 使用存储库获取错误没有为此对象定义无参数构造函数 - Using a Repository getting error No parameterless constructor defined for this object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM