简体   繁体   中英

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. Everything seems working until I post the AddNew Staff Performance form using this view model. 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.

   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. 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.

Source Error:

An unhandled exception was generated during the execution of the current web request. 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: No parameterless constructor defined for this object. 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 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 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 ionStep.Execute() +103 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +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. Once you define a Parametised constructor, you have to provide the parameterless one manually.

It is complaning about there being none.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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