30

I am trying to load data of a user edit it and then save it. this has been working and im not quite sure what i changed but now i am getting the following error...

Value cannot be null.
Parameter name: value

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.ArgumentNullException: Value cannot be null.
Parameter name: value

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: 


[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() +8897857
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184


     public ActionResult EditDetails()
    {
        int id = Convert.ToInt32(Session["user"]);
        S1_Customers u1_users = storeDB.S1_Customers.Find(id);
        return View(u1_users);
    }

    [HttpPost]
    public ActionResult EditDetails(S1_Customers u1_users)
    {
        var Pcode = "";  
        if (ModelState.IsValid)
        {

I am not even reaching ModelState.IsValid when i click submit

1
  • 2
    Please post your model-code, and the action you're calling to save this.. Seems there are some problems with validation, but more information is needed.... Why did this question get a down-vote? Sep 21, 2011 at 13:10

5 Answers 5

27

Did you change any names? The form names have to map 1-1 with your Action parameters. In this case, the "name" parameter was not passed to the controller action, so it is null.

Wild guess, need more information (method signature of action)

6
  • 10
    Ahh yeh silly me it was because i did this... [Display(Name = "")] public string Addrs { get; set; }
    – Beginner
    Sep 21, 2011 at 13:15
  • 5
    I am not sure if it is the same issue but I am getting the same exception if I apply a member with a StringLength attribute and also apply [DisplayName("")]. Oct 10, 2011 at 22:47
  • 1
    Yep, StringLength with DisplayName("") caused the same issue for me as well. Mar 7, 2012 at 13:02
  • I did the same thing, as a cheeky hack to change the text for the jquery validation, guess I'll have to fix it properly now! May 18, 2012 at 19:42
  • An alternative cheeky hack to remove the field name from validation is [Display(Name = " ")]. Note the blank space character.
    – Alex Angas
    Feb 21, 2014 at 6:40
19

You'll receive that error if you have some properties decorated by DisplayAttribute with empty Name ([DisplayAttribute(Name = "", Description = "Any description")])

1
  • Can't believe I spent such a long time before coming to this answer. Jun 26, 2017 at 19:33
14

If you use [Display(Name="")] as for properties of your model, This will cause the error you get. To solve this problem, you should avoid using empty display name attribute.

[Display(Name = "")] //this line is the cause of error
public string PromotionCode { get; set; }
3

It could most probably be that your model has a property that returns a non-nullable value, like int, DateTime, double etc. And if user is updating the entry you are probably not storing that value in a hidden field or somewhere, so when the data is returned that particular property is null. Either place that property into a hidden field or make your property nullable in a model by changing int to int?, etc.

1

I was getting this same error message when manually setting a @Html.TextArea, I had used the code from an @Html.TextBox(null, this.Model) in an EditorTemplate and when I did @Html.TextArea(null, this.Model) I got the error above.

Turns out you have to do @Html.TextArea("", this.Model) and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.