Upgraded MVC 2 in MVC3 gets an exception for the model linker

After updating, I get this error for some types.

Value cannot be null. Parameter name: value 

Stack trace

 [ArgumentNullException: Value cannot be null. Parameter name: value] System.ComponentModel.DataAnnotations.ValidationContext.set_DisplayName(String value) +51903 System.Web.Mvc.<Validate>d__1.MoveNext() +135 System.Web.Mvc.<Validate>d__5.MoveNext() +318 System.Web.Mvc.DefaultModelBinder.OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext) +139 System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +66 System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +1367 System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +449 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +317 System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +117 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343 System.Web.Mvc.Controller.ExecuteCore() +116 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10 System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37 System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21 System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12 System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62 System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50 System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8920029 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184 

Any ideas what the problem might be?

edit: Found a problem,

 [RequiredRequestedOnSiteDate] [DisplayName("")] public Date RequestedOnSiteDate { get; set; } 

In MVC2 this works, we do not want the display name for this text field, since the name is presented in the header. How can I have no display name without error? Thanks

+4
source share
3 answers

Guess...

Instead of using [Required], the DisplayName attribute of your model has a trap inside the setter that throws an exception at the zero value, and the binder tries to set the null property, because that is what is included in the form.

Give us the model code (and possibly the controller action method) so that we can better understand.

0
source

You just need to remove @Html.DisplayFor(m => m.RequestOnSiteDate) from your code and you will never see the name.

0
source

Anders, Here are a few years later, and I have the same error in MVC4.

 [DisplayName("")] public NexEnum.Veteran Veteran { get; set; } 

Have you ever allowed this? I solved it as follows:

 [DisplayName(" ")] public NexEnum.Veteran Veteran { get; set; } 
0
source

All Articles