我正在为用户构建一个注册/登录引擎。除了我在我的模型中使用比较验证之外,一切正常。当它去保存我的新用户实体时,它给了我这个错误。
一个或多个实体的验证失败。有关更多详细信息,请参阅“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);
}