在我的 ASP.NET MVC 应用程序上,我正在尝试更新两个相关模型,如下所示:
if (ModelState.IsValid) {
_accommpropertyseasonrepo.Edit(accommPropertySeasonCreateViewModel.AccommPropertySeason);
_accommpropertyseasonrepo.Save();
_accommpropertyseasondetailrepo.Edit(accommPropertySeasonCreateViewModel.AccommPropertySeasonDetail);
_accommpropertyseasondetailrepo.Save();
return RedirectToAction("Details", new { id = id });
}
第一个通过没有问题,但是当它尝试更新第二个时,并发正在扭结并引发以下错误:
存储更新、插入或删除语句影响了意外数量的行 (0)。自加载实体后,实体可能已被修改或删除。刷新 ObjectStateManager 条目
实现我在这里尝试做的事情的方法是什么?
更新
这是_accommpropertyseasondetailrepo
Edit
方法:
public void Edit(AccommPropertySeasonDetail entity) {
_context.Entry(entity).State = System.Data.EntityState.Modified;
}
这是_accommpropertyseasonrepo
Edit
方法:
public void Edit(AccommPropertySeason entity) {
_context.Entry(entity).State = System.Data.EntityState.Modified;
}
更新 2
这是整个动作方法:
public ActionResult SeasonEdit_post(int id, int subid,
[Bind(Exclude =
"AccommPropertySeason.CreatedBy,AccommPropertySeason.CreatedOn")]
AccommPropertySeasonCreateViewModel accommPropertySeasonCreateViewModel) {
if (ModelState.IsValid) {
_accommpropertyseasonrepo.Edit(accommPropertySeasonCreateViewModel.AccommPropertySeason);
_accommpropertyseasonrepo.Save();
_accommpropertyseasondetailrepo.Edit(accommPropertySeasonCreateViewModel.AccommPropertySeasonDetail);
_accommpropertyseasondetailrepo.Save();
return RedirectToAction("Details", new { id = id });
}
return View(accommPropertySeasonCreateViewModel);
}
更新
残酷的真相:我在视图中忘记了以下代码行,因此模型在没有 PK 的情况下返回:
@Html.HiddenFor(m => m.AccommPropertySeasonDetail.AccommPropertySeasonDetailID)
现在问题解决了!