-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);
}
感謝您的即時回覆。我試圖保存第二個密碼字段的唯一原因是看看是否可以解決錯誤。我的計劃是刪除它..(你是正確的)..我如何記錄錯誤..我知道如何步驟和調試..再次感謝您的時間。 – digiShadoe
http://www.codeproject.com/Articles/140911/log4net-Tutorial –