追查 ModelState.IsValid 错误
asp.net MVC 中有没有一种好方法来跟踪 ModelState 错误?当我提交表单时,IsValid 返回 false,即使其中实际上没有任何字段或任何正在验证的内容。我查看了这里这个线程< /a> 并尝试了 Steve Willcock 的建议,但没有抛出异常,只是一个一般错误。
有没有什么方法可以让我看到是什么导致 ModelState 变得无效,而无需逐步检查所使用的模型的每个部分?它很大,我只是想知道是否有更有效的方法。
Is there a good way in asp.net MVC to trace ModelState errors? IsValid returns false when I submit my form even if there aren't actually any fields in it, or anything being validated. I looked at this thread here and tried Steve Willcock's suggestion but no exceptions are thrown, just a generic error.
Is there any way I can see what is causing the ModelState to become invalid without stepping through every single part of the model that's used? It's quite large and I was just wondering if there is a more efficient way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了这个问题,发现在我的模型中,我有一个 int 类型的唯一 id 导致了问题。当我在模型中将其设置为可为空时,错误消失了。
没有看到你的任何代码,虽然有点难以弄清楚,但上面的内容对我有帮助。
我还注意到,当我使用“书呆子晚餐”方法进行验证时,有时会弹出错误窗口,显示“所需值”之类的内容。将其链接到同一问题并使所有 Id 可为空。
希望这有帮助。
I had this issue and found that in my model I had a unique id of type int that was causing a problem. When I set it to nullable in my model the error went away.
W/out seeing any of your code though it's a bit hard to figure out but the above helped me.
I also noticed that when I was doing validations, using the Nerd Dinner approach, I would sometimes get an error popup saying something like "Value Required". Linked it to the same problem and made all Id's nullable.
Hope this helps.