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

30

我正在尝试加载用户数据并对其进行编辑,然后保存。这一直在运作,但我不太确定我改变了什么,现在我收到以下错误信息...

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)
        {

当我点击提交按钮时,甚至没有到达ModelState.IsValid。


2
请发布您的模型代码和调用保存操作的方法。似乎存在一些验证问题,但需要更多信息...为什么这个问题会被投票否决? - Yngve B-Nilsen
5个回答

27

您是否更改了任何名称?表单名称必须与您的操作参数一一对应。在此情况下,“name”参数未传递给控制器操作,因此为null。

猜测可能性较大,需要更多信息(操作的方法签名)


10
啊,我真傻,是因为我做了这个……[Display(Name = "")]公共字符串Addrs {获取;设置;} - Beginner
5
我不确定是否和原问题一样,但如果我对成员应用了StringLength属性并且还使用了[DisplayName("")],我会收到相同的异常。 - Matthew Nichols
1
是的,使用 DisplayName("") 的 StringLength 对我也造成了同样的问题。 - glennanthonyb
我也做了同样的事情,作为一个调皮的hack来改变jquery验证的文本,现在想必我得好好修复它了! - Chris Barry
一种替代性的聪明方法来从验证中删除字段名称是[Display(Name = " ")]。请注意空格字符。 - Alex Angas
在我的情况下,资源文件中的显示名称的值为空。 - Rodrigo Prieto

20

如果您有一些使用空名称装饰的 DisplayAttribute 的属性,您将收到该错误 ([DisplayAttribute(Name = "", Description = "任意描述")])


真不敢相信在找到这个答案之前我花了这么长时间。 - Vasily Hall

14

如果您在模型的属性中使用[Display(Name="")],则会导致出现错误。要解决此问题,您应避免使用空的显示名称属性。

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

4
很可能是您的模型具有返回非空值的属性,例如 intDateTimedouble 等。如果用户正在更新条目,并且您可能没有将该值存储在隐藏字段或其他地方,则当数据返回时,该特定属性将为空。要么将该属性放入隐藏字段中,要么通过将 int 更改为 int? 等,在模型中使属性可空。

DateTimeOffset 如果不可为空也会失败。 - Javier Contreras

1
当我手动设置@Html.TextArea时,我收到了相同的错误消息。我在EditorTemplate中使用了@Html.TextBox(null, this.Model)的代码,并且当我执行@Html.TextArea(null, this.Model)时,我得到了上面的错误。
结果证明你必须这样做@Html.TextArea("", this.Model),它会工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接