MVC3值不能为空。参数名称:值

时间:2023-01-19 02:15:03

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

我甚至没有达到模型状态。当我点击提交时是有效的。

5 个解决方案

#1


24  

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.

你改名字了吗?表单名称必须用动作参数映射1-1。在这种情况下,“name”参数没有传递给控制器操作,所以它是null。

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

疯狂猜测,需要更多信息(方法签名)

#2


16  

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

如果您使用的是带有空名称的DisplayAttribute修饰的属性,您将会收到这个错误([DisplayAttribute(Name = "", Description = "Any Description ")])

#3


10  

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=")]属性,则会导致错误。要解决这个问题,您应该避免使用空显示名属性。

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

#4


2  

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.

可能最有可能是你的模型有一个属性,它返回一个非空值,如int,DateTime,双等。如果用户更新条目你可能不是存储在一个隐藏字段值或某个地方,所以当特定属性的数据返回null。要么将该属性放入隐藏字段中,要么通过将int改为int使您的属性在模型中为空。等。

#5


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.

在手动设置@Html时,我得到了相同的错误消息。我用了@Html的代码。在编辑器模板中,当我使用@Html时。TextArea(null, this.Model)上面有错误。

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

事实证明,你必须使用@Html。TextArea(", this.Model),它可以工作。

#1


24  

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.

你改名字了吗?表单名称必须用动作参数映射1-1。在这种情况下,“name”参数没有传递给控制器操作,所以它是null。

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

疯狂猜测,需要更多信息(方法签名)

#2


16  

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

如果您使用的是带有空名称的DisplayAttribute修饰的属性,您将会收到这个错误([DisplayAttribute(Name = "", Description = "Any Description ")])

#3


10  

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=")]属性,则会导致错误。要解决这个问题,您应该避免使用空显示名属性。

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

#4


2  

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.

可能最有可能是你的模型有一个属性,它返回一个非空值,如int,DateTime,双等。如果用户更新条目你可能不是存储在一个隐藏字段值或某个地方,所以当特定属性的数据返回null。要么将该属性放入隐藏字段中,要么通过将int改为int使您的属性在模型中为空。等。

#5


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.

在手动设置@Html时,我得到了相同的错误消息。我用了@Html的代码。在编辑器模板中,当我使用@Html时。TextArea(null, this.Model)上面有错误。

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

事实证明,你必须使用@Html。TextArea(", this.Model),它可以工作。