无法删除此对象,因为未在 ObjectStateManager 中找到它。
1 public ActionResult Delete(int id)
2 {
3 var dal = new XikeCompany.Models.XikeMvcEntities();
4 var model = dal.Manager.Where(m => m.ID == id).Single();
5 return View(model);
6 }
7
8 [HttpPost, ActionName("Delete")]
9 public ActionResult DeleteConfirmed(int id)
10 {
11 using (var dal = new XikeCompany.Models.XikeMvcEntities())
12 {
13 var model = dal.Manager.FirstOrDefault(m => m.ID == id);
14 db.DeleteObject(model);
15 db.SaveChanges();
16 return RedirectToAction("Index");
17 }
18 }我已经贴了代码,清大家看看什么原因谢谢了
var model = dal.Manager.FirstOrDefault(m => m.ID == id);
db.AttachTo("ModelSetName", model);
db.DeleteObject(model);
db.SaveChanges();
return RedirectToAction("Index");