我正在尝试更改应用程序用户的属性,但出现错误在
IdentityResult result = await Usermanager.UpdateAsync(model);
对象引用未设置为对象的实例。
目标是改变ApplicationUser
Fullname
、Website
、Email
,如果他愿意的话。有没有更简单的方法可以代替我在这里尝试做的事情?还是我走在正确的轨道上?
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> EditUserData(string FullName, string Website, string Email)
{
ApplicationUser model = db.Users.Find(User.Identity.GetUserId());
model.Email = Email;
model.UserName = Email;
model.FullName = FullName;
model.Website = Website;
IdentityResult result = await Usermanager.UpdateAsync(model);
return RedirectToAction("Settings","Admin");
}
尝试一些东西
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> EditUserData(ApplicationUser user)
{
UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(db);
UserManager<ApplicationUser> UserManager = new UserManager<ApplicationUser>(store);
ApplicationUser model = UserManager.FindById(User.Identity.GetUserId());
model.Email = user.Email;
model.UserName = user.UserName;
model.FullName = user.FullName;
model.Website = user.Website;
IdentityResult result = await UserManager.UpdateAsync(model);
//UserManager.Update(model);
//db.SaveChanges();
return RedirectToAction("Settings", "Admin");
}
页面只是刷新。并且未进行任何更改
我认为
,没有传递操作参数。如果要应用 post,则应将参数合并为复杂对象。那么,为什么不使用ApplicationUser
作为参数。
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> EditUserData(ApplicationUser user)
{
ApplicationUser model = db.Users.Find(User.Identity.GetUserId());
model.Email = user.Email;
model.UserName = user.UserName;
model.FullName = user.FullName;
model.Website = user.Website;
IdentityResult result = await Usermanager.UpdateAsync(model);
return RedirectToAction("Settings","Admin");
}