ASP.NET MVC 模型状态
ModelState.IsValid 在我的控制器中为我返回 false 。我知道这意味着模型绑定时发现一个或多个模型错误。我的问题是如何查看错误?
我注意到我的特定 ModelState 有 6 个项目。如果我尝试执行其中任何操作...
ModelState[0].Errors[0].ToString()
ModelState[0].Errors[0].ErrorMessage
ModelState[0].Value.AttemptedValue
我会收到此错误:
The best overloaded method match for 'System.Web.Mvc.ModelStateDictionary.this[string]' has some invalid arguments
ModelState.IsValid is returning false for me in my controller. I know that means one or more model errors were found when model binding. My question is how do I see the errors?
I noticed that my particular ModelState has 6 items in it. If I try to do any of these...
ModelState[0].Errors[0].ToString()
ModelState[0].Errors[0].ErrorMessage
ModelState[0].Value.AttemptedValue
I get this error:
The best overloaded method match for 'System.Web.Mvc.ModelStateDictionary.this[string]' has some invalid arguments
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ModelState 的索引器是一个字符串(通常是有问题的模型的属性名称或 html 元素的名称)。
如果您检查 MSDN 文档中的 ModelState 类,您将看到它有一个 Errors 集合,允许您迭代错误项(它们是 ModelError 实例)以查看导致它们的原因。
The indexor into the ModelState is a string (usually the property name of the offending model or name of the html element).
If you check the MSDN docs for the ModelState class, you'll see that it has an Errors collection that will allow you to iterate over the error items (which are ModelError instances) to see what caused them.
在控制器中;
在视野中;
Html.ValidationSummary()也
可能是您正在寻找的。
in controller;
in view;
also
Html.ValidationSummary() might be what you are looking for.