如何从 ASP.Net MVC modelState 获取所有错误?
我想在不知道键值的情况下从 modelState 中获取所有错误消息。循环获取 ModelState 包含的所有错误消息。
我该怎么做?
I want to get all the error messages out of the modelState without knowing the key values. Looping through to grab all the error messages that the ModelState contains.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(22)
使用 LINQ:
Using LINQ:
另请参阅如何做我得到了 ASP.NET MVC 中模型状态错误的集合?。
See also How do I get the collection of Model State Errors in ASP.NET MVC?.
在 LINQ 版本的基础上,如果要将所有错误消息连接到一个字符串中:
Building on the LINQ verison, if you want to join all the error messages into one string:
我可以使用一点 LINQ 来完成此操作,
上面的方法返回验证错误列表。
进一步阅读:
如何从 ASP.NET MVC 中的 ModelState 读取所有错误
I was able to do this using a little LINQ,
The above method returns a list of validation errors.
Further Reading :
How to read all errors from ModelState in ASP.NET MVC
在调试过程中,我发现在每个页面的底部放置一个表格来显示所有 ModelState 错误很有用。
During debugging I find it useful to put a table at the bottom of each of my pages to show all ModelState errors.
正如我发现遵循到目前为止给出的答案中的建议,您可以在没有设置错误消息的情况下发生异常,因此要捕获所有问题,您确实需要同时获取 ErrorMessage 和 Exception。
或作为扩展方法
As I discovered having followed the advice in the answers given so far, you can get exceptions occuring without error messages being set, so to catch all problems you really need to get both the ErrorMessage and the Exception.
or as an extension method
如果有人想要返回模型属性的名称以将错误消息绑定到强类型视图中。
这样,您实际上可以将错误与引发错误的字段联系起来。
In case anyone wants to return the Name of the Model property for binding the error message in a strongly typed view.
This way you can actually tie the error in with the field that threw the error.
此代码片段也很有用,它为您提供一个包含错误消息的列表。
var error = ModelState.Values.SelectMany(x => x.Errors.Select(c => c.ErrorMessage)).ToList();
This code snippet is useful too and give you a List that contains of Error Messges.
var errors = ModelState.Values.SelectMany(x => x.Errors.Select(c => c.ErrorMessage)).ToList();
仅输出错误消息本身对我来说还不够,但这确实解决了问题。
Outputting just the Error messages themselves wasn't sufficient for me, but this did the trick.
任何人都在寻找 asp.net core 3.1。答案包括键(字段名称)。大多数其他答案仅包含错误。
我发现这就是 [ApiController] 返回的
Anybody looking for asp.net core 3.1. The answer includes key (field name). Most of other answers include only errors.
I found that this is what [ApiController] returns
为了以防万一有人需要它,我在我的项目中创建并使用以下静态类
使用示例:
用途:
类:
For just in case someone need it i made and use the following static class in my projects
Usage example:
Usings:
Class:
只需使用 asp-validation-summary 标签帮助程序
simply use asp-validation-summary Tag Helper
这也有效:
And this works too:
对于将错误消息数组传递给 View 很有用,也许可以通过 Json:
Useful for passing array of error messages to View, perhaps via Json:
这是对@Dunc 答案的扩展。请参阅 xml 文档注释
This is expanding upon the answer from @Dunc . See xml doc comments
对于 AJAX 请求更好的解决方案:
响应格式将是:
有关 Func<> 的更多详细信息检查此页面: Func< TSource,Int32,TResult>)
For AJAX Request better solution:
Response format will be:
For more details about Func<> check this page : Func<TSource,Int32,TResult>)
此外,
ModelState.Values.ErrorMessage
可能为空,但ModelState.Values.Exception.Message
可能指示错误。In addition,
ModelState.Values.ErrorMessage
may be empty, butModelState.Values.Exception.Message
may indicate an error.获取带有字段名称和错误消息的错误
错误模型
get error with Field Name and Error Message
Error Model
使用字段名称..
With fieldName ..
在您的实现中,您缺少静态类,这应该是。
而是
In your implementation you are missing static Class, this should be.
rather
var result = string.Join(',',ModelState.Values.SelectMany(v => v.Errors).Select(a=>a.ErrorMessage));
var result = string.Join(',',ModelState.Values.SelectMany(v => v.Errors).Select(a=>a.ErrorMessage));