ModelState.IsValid == false,为什么?

发布于 2024-08-12 10:33:43 字数 64 浏览 5 评论 0原文

在哪里可以找到导致 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

ゝ偶尔ゞ 2024-08-19 10:33:43

由于您可能正在 Visual Studio 中进行编程,因此您最好利用使用断点的可能性来进行如此简单的调试步骤(了解您的情况下的问题是什么)。只需将它们放在检查 ModelState.isValid 的前面/位置并将鼠标悬停在 ModelState 上即可。现在您可以轻松浏览里面的所有值,看看是什么错误导致 isvalid 返回 false。

modelstate

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.

modelstate

颜漓半夏 2024-08-19 10:33:43

将以下代码粘贴到控制器的 ActionResult 中,并将调试器放置在此时。

var errors = ModelState
    .Where(x => x.Value.Errors.Count > 0)
    .Select(x => new { x.Key, x.Value.Errors })
    .ToArray();

Paste the below code in the ActionResult of your controller and place the debugger at this point.

var errors = ModelState
    .Where(x => x.Value.Errors.Count > 0)
    .Select(x => new { x.Key, x.Value.Errors })
    .ToArray();
感性 2024-08-19 10:33:43

关于“难道有 0 个错误并且 IsValid == false”:这是来自 https://github.com/Microsoft/referencesource/blob/master/System.Web/ModelBinding/ModelStateDictionary.cs#L37-L41

public bool IsValid {
    get {
        return Values.All(modelState => modelState.Errors.Count == 0);
    }
}

现在,看起来像这样不可能。嗯,这适用于 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

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.

原野 2024-08-19 10:33:43
bool hasErrors =  ViewData.ModelState.Values.Any(x => x.Errors.Count > 1);

或迭代

    foreach (ModelState state in ViewData.ModelState.Values.Where(x => x.Errors.Count > 0))
    {

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

    }
扬花落满肩 2024-08-19 10:33:43

有时绑定器会抛出异常,但没有错误消息。
您可以使用以下代码片段检索异常以找出问题所在:(

通常如果绑定器尝试将字符串转换为复杂类型等)

 if (!ModelState.IsValid)
            {
var errors = ModelState.SelectMany(x => x.Value.Errors.Select(z => z.Exception));

// Breakpoint, Log or examine the list with Exceptions.

  }

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.

  }
臻嫒无言 2024-08-19 10:33:43

如果您删除对 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.

清浅ˋ旧时光 2024-08-19 10:33:43

控制器上的 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.

稚然 2024-08-19 10:33:43

正如我刚刚发生的那样 - 当您向模型添加所需的属性而不更新表单时,也会发生这种情况。在这种情况下,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.

笑,眼淚并存 2024-08-19 10:33:43

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

enter image description here

清风挽心 2024-08-19 10:33:43

我将一些 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文