Pan*_*yay 1 c# entity-framework-4.1 asp.net-mvc-3
我的创建和删除操作运行良好.但是,编辑功能会出现以下错误: - 存储更新,插入或删除语句影响了意外的行数(0).自实体加载后,实体可能已被修改或删除.刷新ObjectStateManager条目.
注意: - CatPicView是一个ViewModel合并两个实体Category和Picture
以下是我的编辑操作代码的代码: -
[HttpPost]
public ActionResult Edit(CatPicView catPic)
{
if (ModelState.IsValid)
{
if (!String.IsNullOrEmpty(catPic.Picture.PictureUrl))
{
if (catPic.Category.PictureId == null)
{
Picture picture = new Picture();
picture.PictureUrl = catPic.Picture.PictureUrl;
db.Pictures.Add(picture);
catPic.Category.PictureId = picture.Id;
}
else
{
db.Entry(catPic.Picture).State = EntityState.Modified;
}
}
db.Entry(catPic.Category).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ParentCategoryId = new SelectList(db.Categories, "Id", "Name", catPic.Category.ParentCategoryId);
return View(catPic);
}
Run Code Online (Sandbox Code Playgroud)
使用debuger,检查您要更新的对象的属性.最有可能的是你的ID为null.
要解决此问题,您应该放置一个隐藏字段,其中包含已修改对象的ID,因此在发布表单时,它会映射到您的对象.
| 归档时间: |
|
| 查看次数: |
1097 次 |
| 最近记录: |