당신은 바인딩 모델 중에이이 NullReferenceException
와 충돌합니다 모델 클래스에 다음과 같은 두 가지 속성을 정의하는 경우 :정지 ASP.NET MVC 모델 클래스의 모든 게터를 호출
public Customer Customer { get; private set; } //set in the action method
public bool Name => Customer.Name;
이것은 Customer
이 여전히 있기 때문에 모델 바인딩 중에는 null이고 ASP.NET MVC에서는 Name
에 대한 getter를 호출합니다.
스택은 다음과 같습니다이 모델 검증과 같은 스택에서
System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component) +525
System.Web.Mvc.ModelMetadata.get_Model() +34
System.Web.Mvc.DataAnnotationsModelValidator.Validate(Object container) +151
System.Web.Mvc.<Validate>d__1.MoveNext() +387
System.Web.Mvc.DefaultModelBinder.OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext) +163
System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +83
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +1754
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +460
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +137
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +982
System.Web.Mvc.<>c__DisplayClass22.<BeginExecuteCore>b__1e() +39
System.Web.Mvc.Async.AsyncResultWrapper.<.cctor>b__0(IAsyncResult asyncResult, Action action) +21
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +44
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +65
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +399
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +137
이
모든 게터를 조회한다. 모델 유효성 검사를 사용하지 않습니다.
이 상황을 어떻게 처리 할 수 있습니까? ASP.NET MVC가 (명백한) 이유없이 모든 getter를 호출하지 않도록 할 수 있습니까?
"왜 이러는가?"외에이 모델의 사용법을 보여줄 수 있습니까? 이 예외가 정확히 발생합니까? – CodeCaster
'ActionResult M (MyModel 모델) {m.Customer = ...; 반환보기 (모델); }'. 폼 페이지에서이 패턴이 편리한 패턴임을 알 수 있습니다. – boot4life
모델 바인딩 중 예외가 발생합니다. 내 코드가 실행되기 전에. 스택을 참조하십시오. – boot4life