2013-09-21 121 views
-1

我正在爲用戶構建註冊/登錄引擎。除了我在我的模型中使用Compare驗證外,一切正常。當它去保存我的新用戶實體它給我這個錯誤。比較使用Dataanotations導致驗證失敗的字段驗證

一個或多個實體的驗證失敗。有關更多詳細信息,請參閱「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); 
    } 

回答

2

這個錯誤不是由您的Compare一個引起ttribute - 這是在嘗試將數據保存到數據庫時發生的驗證異常。您可以捕獲該異常,並整理這樣的實體驗證錯誤:

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的消息會告訴你的集合問題(S)是/是什麼。

但是,由於問題似乎與ConfirmPassword屬性 - 問題必須提出;爲什麼你要存儲這個呢?你的用戶真的需要將他們的密碼保存在兩個不同的列中嗎?尤其是因爲這似乎是密碼的明文版本,這意味着您的加密是毫無意義的。

+0

感謝您的即時回覆。我試圖保存第二個密碼字段的唯一原因是看看是否可以解決錯誤。我的計劃是刪除它..(你是正確的)..我如何記錄錯誤..我知道如何步驟和調試..再次感謝您的時間。 – digiShadoe

+1

http://www.codeproject.com/Articles/140911/log4net-Tutorial –