C# ModelState.IsValid == false,为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1791570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ModelState.IsValid == false, why?
提问by Omu
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.
在哪里可以找到导致 ModelState 无效的错误列表?我没有在 ModelState 对象上看到任何错误属性。
采纳答案by queen3
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
关于“是否有 0 个错误和 IsValid == false”:这里是来自https://github.com/Microsoft/referencesource/blob/master/System.Web/ModelBinding/ModelStateDictionary.cs#L37-L41的 MVC 源代码
public bool IsValid {
get {
return Values.All(modelState => modelState.Errors.Count == 0);
}
}
Now, it looks like it can't be. Well, that's for ASP.NET MVC v1.
现在看来,不可能了。嗯,这适用于 ASP.NET MVC v1。
回答by tvanfosson
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.
控制器上的 ModelState 属性实际上是一个 ModelStateDictionary 对象。您可以遍历字典上的键并使用 IsValidField 方法检查该特定字段是否有效。
回答by Michael G
bool hasErrors = ViewData.ModelState.Values.Any(x => x.Errors.Count > 1);
or iterate with
或迭代
foreach (ModelState state in ViewData.ModelState.Values.Where(x => x.Errors.Count > 0))
{
}
回答by bastijn
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.
由于您可能正在 Visual Studio 中编程,因此您最好利用使用断点进行此类简单调试步骤的可能性(了解您的情况是什么问题)。只需将它们放在您检查 ModelState.isValid 的位置的前面 / 并将鼠标悬停在 ModelState 上。现在您可以轻松浏览其中的所有值,并查看导致 isvalid 返回 false 的错误。
回答by Jonas Stensved
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)
(通常如果活页夹试图将字符串转换为复杂类型等)
if (!ModelState.IsValid)
{
var errors = ModelState.SelectMany(x => x.Value.Errors.Select(z => z.Exception));
// Breakpoint, Log or examine the list with Exceptions.
}
回答by AndyP9
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.
正如我刚刚发生的那样 - 当您向模型添加必需的属性而不更新表单时,也会发生这种情况。在这种情况下,ValidationSummary 不会列出错误消息。
回答by Krishna
Paste the below code in the ActionResult of your controller and place the debugger at this point.
将以下代码粘贴到控制器的 ActionResult 中,并将调试器放置在此处。
var errors = ModelState
.Where(x => x.Value.Errors.Count > 0)
.Select(x => new { x.Key, x.Value.Errors })
.ToArray();
回答by Tom McDonough
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.
如果您删除对 ModelsState.IsValid 的检查并让它出错,如果您复制此行((System.Data.Entity.Validation.DbEntityValidationException)$exception).EntityValidationErrors
并将其粘贴到 Visual Studio 的 watch 部分,它将准确地告诉您错误是什么。节省大量时间检查错误所在。