c# - 验证失败时如何检查 EntityValidationErrors?

标签 c# asp.net asp.net-mvc entity-framework asp.net-mvc-5

当我尝试在 MVC 4 数据库第一个项目中编辑属性时收到此消息。我正在使用 MVC 默认编辑页面。

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

我在哪里检查验证?

最佳答案

转到您的编辑功能,放置一个 try - catch block 并捕获异常 - 'DbEntityValidationException'

如果您想查看错误,请遍历验证错误。

这是一个简单的代码示例。

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

关于c# - 验证失败时如何检查 EntityValidationErrors?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17020947/

相关文章:

c# - T 上的泛型约束同时是引用类型和值类型?

javascript - Controller 将 ViewBag 模型拆分为具有数据库参数的多个页面正在跳过记录

asp.net-mvc - 使用 MVC 时,如何调用 Controller Action 并传递文本框值?

c# - 输入字符串的格式不正确

asp.net-mvc - @Html.CheckboxFor 不在 mvc 中显示

c# - 使用 ASP.NET 清除缓存的最有效方法

c# - 在 Windows Phone 8 上平滑缩放和平移

c# - LESS 到 CSS 的转换是在客户端还是在服务器端完成的?

c# - ASP.NET 服务器控件错误 : Unknown server tag

c# - Linq 返回意外结果