我正在尝试加载用户的数据编辑它然后保存它。这一直在工作,我不太确定我改变了什么,但现在我收到以下错误......
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
答案 0 :(得分:26)
你有没有更改任何名字?表单名称必须与您的Action参数映射1-1。在这种情况下,“name”参数未传递给控制器操作,因此它为null。
狂野猜测,需要更多信息(行动方法签名)
答案 1 :(得分:17)
如果您使用带有空名称的DisplayAttribute修饰了某些属性,则会收到该错误
([DisplayAttribute(Name = "", Description = "Any description")])
答案 2 :(得分:11)
如果您使用[显示(名称=&#34;&#34;)]作为模型的属性,这将导致您获得的错误。要解决此问题,应避免使用空显示名称属性。
[Display(Name = "")] //this line is the cause of error
public string PromotionCode { get; set; }
答案 3 :(得分:2)
很可能是你的模型有一个返回不可为空的值的属性,如int
,DateTime
,double
等。如果用户正在更新条目你可能没有将该值存储在隐藏字段或某处,因此当返回数据时,该特定属性为null。要么将该属性放入隐藏字段,要么通过将int更改为int?等使您的属性在模型中可为空。
答案 4 :(得分:1)
手动设置@Html.TextArea
时收到同样的错误消息,我在EditorTemplate中使用了@Html.TextBox(null, this.Model)
的代码,当我@Html.TextArea(null, this.Model)
时,我得到了上面的错误。
原来你必须做@Html.TextArea("", this.Model)
并且它有效。