我的模型如下:
public class ApplicationUser
{
public Int64 Id { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
我的控制器具有编辑模型的操作,如下所示:
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);
}
此处,POST方法正在更新 FullName 和电子邮件字段。但是,它将NULL值更新为密码字段。怎么避免这个?
答案 0 :(得分:1)
此处绑定属性不会帮助您将此行放在savechanges()
db.Entry(model).Property(x => x.Password).IsModified = false