ModelState.IsValid == false,为什么?
在哪里可以找到导致 ModelState 无效的错误列表?我在 ModelState 对象上没有看到任何错误属性。
Where can I find the list of errors of which make the ModelState invalid? I didn't see any errors property on the ModelState object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
由于您可能正在 Visual Studio 中进行编程,因此您最好利用使用断点的可能性来进行如此简单的调试步骤(了解您的情况下的问题是什么)。只需将它们放在检查 ModelState.isValid 的前面/位置并将鼠标悬停在 ModelState 上即可。现在您可以轻松浏览里面的所有值,看看是什么错误导致 isvalid 返回 false。
As you are probably programming in Visual studio you'd better take advantage of the possibility of using breakpoints for such easy debugging steps (getting an idea what the problem is as in your case). Just place them just in front / at the place where you check ModelState.isValid and hover over the ModelState. Now you can easily browse through all the values inside and see what error causes the isvalid return false.
将以下代码粘贴到控制器的 ActionResult 中,并将调试器放置在此时。
Paste the below code in the ActionResult of your controller and place the debugger at this point.
关于“难道有 0 个错误并且 IsValid == false”:这是来自 https://github.com/Microsoft/referencesource/blob/master/System.Web/ModelBinding/ModelStateDictionary.cs#L37-L41
现在,看起来像这样不可能。嗯,这适用于 ASP.NET MVC v1。
About "can it be that 0 errors and IsValid == false": here's MVC source code from https://github.com/Microsoft/referencesource/blob/master/System.Web/ModelBinding/ModelStateDictionary.cs#L37-L41
Now, it looks like it can't be. Well, that's for ASP.NET MVC v1.
或迭代
or iterate with
有时绑定器会抛出异常,但没有错误消息。
您可以使用以下代码片段检索异常以找出问题所在:(
通常如果绑定器尝试将字符串转换为复杂类型等)
Sometimes a binder throwns an exception with no error message.
You can retrieve the exception with the following snippet to find out whats wrong:
(Often if the binder is trying to convert strings to complex types etc)
如果您删除对 ModelsState.IsValid 的检查并让它出错,如果您复制此行
((System.Data.Entity.Validation.DbEntityValidationException)$exception).EntityValidationErrors
并将其粘贴到手表中Visual Studio 中的部分它将准确地告诉您错误是什么。节省大量时间检查错误所在。If you remove the check for the ModelsState.IsValid and let it error, if you copy this line
((System.Data.Entity.Validation.DbEntityValidationException)$exception).EntityValidationErrors
and paste it in the watch section in Visual Studio it will give you exactly what the error is. Saves a lot of time checking where the error is.控制器上的 ModelState 属性实际上是一个 ModelStateDictionary 对象。您可以迭代字典上的键并使用 IsValidField 方法来检查该特定字段是否有效。
The ModelState property on the controller is actually a ModelStateDictionary object. You can iterate through the keys on the dictionary and use the IsValidField method to check if that particular field is valid.
正如我刚刚发生的那样 - 当您向模型添加所需的属性而不更新表单时,也会发生这种情况。在这种情况下,ValidationSummary 将不会列出错误消息。
As has just happened to me - this can also happen when you add a required property to your model without updating your form. In this case the ValidationSummary will not list the error message.
Visual Studio 2022 + dotnet 6更新:
我有同样的问题很长一段时间,终于找到了。就我而言,它是 Id 字段:)
只需放置一个断点并在运行时检查您的 ModelState 并转到此部分:
ModelState ->根->孩子们
,您将看到所有有效和无效的密钥
Visual Studio 2022 + dotnet 6 update:
I had the same problem for a long time and finally I found it. In my case, it was the Id field :)
Just place a breakpoint and check your ModelState in runtime and go to this section :
ModelState -> Root -> Children
and you will see all valid and invalid Keys
我将一些 JSON 粘贴到 MS Teams Wiki 页面中以供将来参考,当我将其复制回来使用时,它添加了额外的不可见字符。我通过在 JSONLint 上检查它来确认这一点
,删除多余的字符为我解决了这个错误。
I pasted some JSON into MS Teams Wiki page for future reference, when I copied it back out for use, it added extra invisible characters. I confirmed this by linting it at JSONLint
Removing the extra characters fixed this error for me.