![](/img/trans.png)
[英]Error “No parameterless constructor defined for this object.” with SelectList from ViewModel
[英]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.WrappedAsyncVoid1.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。 WrappedAsyncVoid1.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.WrappedAsyncVoid1.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.