简体   繁体   中英

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?

For Example:

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

Description

You can loop through the ModelStateValues collection

Sample

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

More Information

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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