-1

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

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

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

我的模型:

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);
    }
4

1 回答 1

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财产上 - 必须提出这个问题;你为什么要存储这个呢?您的用户真的需要将密码保存在两个不同的列中吗?特别是因为这似乎是他们密码的明文版本,这意味着您的加密毫无意义。

于 2013-09-21T18:32:51.880 回答