How do I check EntityValidationErrors when validation fails?

試著忘記壹切 提交于 2019-12-10 04:13:29

问题


I get this message when I try to edit a property in MVC 4 database first project. I'm using the MVC default edit page.

"Validation failed for one or more entities. See "EntityValidationErrors" property for more details."

Where do I check for validation?


回答1:


Go to your edit function, put a try - catch block and catch the exception - 'DbEntityValidationException'

if you want to see the errors, iterate though the validation errors.

here is a simple code example.

catch (DbEntityValidationException ex)
{
    foreach (var errors in ex.EntityValidationErrors)
    {
        foreach (var validationError in errors.ValidationErrors)
        {
             // get the error message 
            string errorMessage = validationError.ErrorMessage;
        }
    }
}



回答2:


If you set a break point in your controller you can check which values have errors against them by looking in the ModelState. The ModelState.Values collection contains the error and the key is the field.



来源:https://stackoverflow.com/questions/17020947/how-do-i-check-entityvalidationerrors-when-validation-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!