无法访问ApplicationUser属性BizName,因为在尝试更新编辑后方法时ApplicationUser始终为null。空异常是@行ApplcationUser.BusinessName;
我尝试了
var userFromDb = _db.ApplicationUser.Include(u => u.Customer).Where(u => u.Id == id).FirstOrDefault();
和
var userFromDb = _db.Customer.Include(u => u.ApplicationUser).Where(u => u.CustomerUserId == id).FirstOrDefault();
控制器
[HttpPost, ActionName("Edit")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> EditUser(string id, ApplicationUser applicationUser)
{
if (id != applicationUser.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
var userFromDb = _db.ApplicationUser.Include(u => u.Customer).Where(u => u.Id == id).FirstOrDefault();
....
_db.SaveChanges();
return RedirectToAction(nameof(Index));
}
return View(applicationUser);
}
型号
public class Customer
{
[Key, ForeignKey("ApplicationUser")]
public string CustomerUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public string BizName
{
get
{
return ApplicationUser.BusinessName;
}
}
}