[HttpPost]
public ActionResult Edit(ApplicationUser user, string password)
{
if(password.IsEmpty() == false)
{
user.PasswordHash = userManager.PasswordHasher.HashPassword(password);
}
user.Status = Enums.DataStatus.Updated;
var result =userManager.Update(user); //error!!
return RedirectToAction("EmployeeList");
}
如果我在发布编辑时更改UserName变量,我不会得到错误,但是当我使用它而不更改它时,它会给出错误。我如何修复它(我想通过改变其他属性而不改变用户名来使用它)
您必须首先将该用户附加到现有用户。
[HttpPost]
public ActionResult Edit(ApplicationUser user, string password)
{
var Existinguser = await UserManager.FindByNameAsync(user.userName);
if(password.IsEmpty() == false)
{
Existinguser.PasswordHash = userManager.PasswordHasher.HashPassword(password);
}
Existinguser.Status = Enums.DataStatus.Updated;
//...
//Update the existing user here
// ExistingUser.Address = user.Address;
//..
var result =userManager.Update(ExistingUser); //Update existing user here
return RedirectToAction("EmployeeList");
}