I am new to asp.net mvc 4 and EF. I am creating User Model which creates/updates user info. The code for model is as follows:
....
public int UserID { get; set; }
[Required]
public string UserName { get; set; }
[Required]
public string Password { get; set; }
[Compare("Password")]
public string VerifyPassword { get; set; }
.....
SOME OTHER FIELDS COMES HERE
......
This same model used for create/update activity. This works fine for create user but while updating i don't want user to update his Id, username so I keep it readonly also don't want to show user password/verify password fields so I removed it from view. Now while updating(Edit mode) I am always getting validation error message for password field. So my question is how do I avoid updating these fields and do update for other fields. To solve this problem I try to create viewmodel but it doesn't help me. Also added code for bypassing validation like this in controller:
public ActionResult Edit(User user)
{
ModelState.Remove("Password");
ModelState.Remove("VerifyPassword");
ModelState.Clear();
if (ModelState.IsValid)
{
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
}
This give me ModelState.IsValid = true but fails to update. Please give me solution to this problem.
Thanks in advance..