I'm building my site based on the patterns used in the ASP.NET Music Store project but I'm having some trouble with editing user records.
My view uses a PasswordFor
for the password field and there are some fields that I need my site to update without any possible user intervention (for auditing).
My problem is that because I'm not supplying a value for password or for the auditing fields, they're submitting with null values.
Here's my POST:
[HttpPost]
public ActionResult Edit(User user)
{
if (ModelState.IsValid)
{
try
{
// supply the Company and Role information by the PK values submitted on the view
user.Company = db.Companies.Single(x => x.CompanyId == user.CompanyId);
user.Role = db.Roles.Single(x => x.RoleId == user.RoleId);
// add the auditing information
user.ModifiedBy = String.Format("{0} {1}", Convert.ToString(Session["firstname"]), Convert.ToString(Session["lastname"]));
user.ModifiedOn = DateTime.Now;
// save the record
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
return Json(new
{
Message = "Success",
IsOK = bool.TrueString
});
}
catch (Exception ex)
{
if (Request.IsAjaxRequest())
{
ThrowError(ex, "EDIT");
}
else
{
return RedirectToAction("Index");
}
}
}
return View(user);
}
So when I submit, because I don't want to change the password, I'm leaving it blank which means the model is getting a null value.
How can I ensure that the fields that I'm not changing aren't going to get updated?