ASP.NET MVC模型状态 [英] ASP.NET MVC Model State

查看:154
本文介绍了ASP.NET MVC模型状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ModelState.IsValid在我的控制器返回给我假的。我知道这意味着找到一个或多个模型错误,当模型绑定。我的问题是我怎么看到错误?

我注意到我的特殊的ModelState中有6个项目。如果我尝试做这些...

 的ModelState [0] .Errors [0]的ToString()
的ModelState [0] .Errors [0] .ErrorMessage
的ModelState [0] .Value.AttemptedValue

我得到这个错误:

 则是'System.Web.Mvc.ModelStateDictionary.this [字符串]'最佳重载的方法匹配具有一些无效参数


解决方案

该indexor到的ModelState是一个字符串(通常是HTML元素的违规型号或名称的属性名)。

如果您检查MSDN文档的的ModelState 类,你会看到它有一个错误集合,将允许您遍历误差项(这是的 ModelError 实例),看看是什么原因导致他们。

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

解决方案

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.

这篇关于ASP.NET MVC模型状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆