I have a Model as follows:
public class ApplicationUser
{
public Int64 Id { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
And my Controller has Actions to Edit the Model as follows:
public ActionResult MyProfile(int id)
{
var currentUser = _context.ApplicationUsers.Find(applicationuser.Id);
return View(currentUser);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult MyProfile([Bind(Exclude="Password")]ApplicationUser applicationuser)
{
if (ModelState.IsValid)
{
_context.Entry(applicationuser).State = EntityState.Modified;
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(applicationuser);
}
Here, POST method is updating the FullName and Email fields properly. But, It's updating NULL value to Password field. How to avoid this?
Here bind attribute'll not help you out put this line before savechanges()
db.Entry(model).Property(x => x.Password).IsModified = false
来源:https://stackoverflow.com/questions/45945832/field-is-updating-with-null-value-even-though-it-is-excluded-from-propery-bindin