简体   繁体   English

枚举代​​码隐藏中的MVC模型验证错误列表

[英]Enumerate list of MVC Model validation errors in code-behind

Within MVC 3, is there a mechanism that allows the enumeration of all validation errors before returning the resulting View? 在MVC 3中,是否有一种机制允许在返回结果View之前枚举所有验证错误?

For Example: 例如:

if (!ModelState.IsValid)
{
    //Enumerate validation errors
}

Description 描述

You can loop through the ModelStateValues collection 您可以遍历ModelStateValues集合

Sample 样品

if (!ModelState.IsValid)
{
    foreach (ModelState modelState in ModelState.Values) {
        foreach (ModelError error in modelState.Errors) {
            // error.ErrorMessage contains the error message
        }
    }
}

More Information 更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM