使用导致验证失败的Dataannotations比较字段验证

时间:2013-09-21 18:22:27

标签: asp.net-mvc-4

我正在为用户构建注册/登录引擎..除了我在模型中使用比较验证外,一切正常。当它保存我的新用户实体时,它会给我这个错误。

一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性。

我知道这是比较验证,因为如果我删除该属性,我的系统工作正常..还有一个很好的方法为我的密码重置问题添加DropDown列表..提前谢谢你,这是我的代码...请帮忙..

我的模特:

public class User
{
    public int UserID { get; set; }

    [Required(ErrorMessage = "A User Name is required max 10 characters")]
    [StringLength(10)]
    public string UserName { get; set; }

    [Required(ErrorMessage = "A First Name is required")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Last Name is required")]
    public string LastName { get; set; }

    [Required]
    [DataType(DataType.EmailAddress, ErrorMessage = "Invalid Email Address")]
    public string Email { get; set; }


    [Required(ErrorMessage = "Password is required")]
    [DataType(DataType.Password, ErrorMessage = "Invalid Password")]
    public string Password { get; set; }

    [Compare("Password")]
    [Display(Name = "Confirmation Password")]
    public string ConfirmPassword { get; set; }

    public string PasswordSalt { get; set; }

    [Required(ErrorMessage = "Password Reset Question required")]
    public string PasswordHint { get; set; }

    [Required(ErrorMessage = "Password Reset Question Answer required")]
    public string PasswordHintAnswer { get; set; }
    public String UserRole { get; set; }


}

我的控制器:

public ActionResult Registration()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Registration(FacultyScheduler.Models.User user)
    {
        if (ModelState.IsValid)
        {
            using (var db = new FacultyContext())
            {

                var crypto = new SimpleCrypto.PBKDF2();
                var encrypPass = crypto.Compute(user.Password);
                var sysUser = db.Users.Create();


                sysUser.UserName = user.UserName;
                sysUser.Email = user.Email;
                sysUser.UserID = user.UserID;
                sysUser.LastName = user.LastName;
                sysUser.FirstName = user.FirstName;
                sysUser.Password = encrypPass;
                sysUser.ConfirmPassword = user.ConfirmPassword;
                sysUser.PasswordSalt = crypto.Salt;
                sysUser.PasswordHint = user.PasswordHint;
                sysUser.PasswordHintAnswer = user.PasswordHintAnswer;
                db.Users.Add(sysUser);
                db.SaveChanges();

                return RedirectToAction("Index", "Home");
            }


        }

        return View(user);
    }

1 个答案:

答案 0 :(得分:2)

此错误不是由Compare属性引起的 - 它是在尝试将数据保存到数据库时发生的验证异常。您可以捕获异常并整理实体验证错误,如下所示:

try
{
    // Your code
}
catch (DbEntityValidatidationException ex)
{
    var errors = ex.EntityValidationErrors
                .SelectMany(o => o.ValidationErrors)
                .Select(o => o.ErrorMessage);

    // Log errors out or just debug and inspect them
}

检查errors是否有一系列信息可以告诉您问题是什么。

但是,由于问题似乎与ConfirmPassword属性有关 - 必须提出问题;为什么要存储这个呢?您的用户是否真的需要将密码保存在两个不同的列中?特别是因为这似乎是他们密码的纯文本版本,这意味着你的加密是毫无意义的。