// The id parameter name should match the DataKeyNames value set on the control public void ListViewMessages_DeleteItem(int id) { var db = new ApplicationDbContext(); ChatApplication.Models.Message item = db.Messages.Find(id); if (item == null) { ModelState.AddModelError("", String.Format("Item with id {0} was not found", id)); return; } db.Messages.Remove(item); db.SaveChanges(); }
// The id parameter name should match the DataKeyNames value set on the control public void ListViewMessages_UpdateItem(int id) { var db = new ApplicationDbContext(); ChatApplication.Models.Message item = null; // Load the item here, e.g. item = MyDataLayer.Find(id); item = db.Messages.Find(id); if (item == null) { // The item wasn't found ModelState.AddModelError("", String.Format("Item with id {0} was not found", id)); return; } TryUpdateModel(item); if (ModelState.IsValid) { // Save changes here, e.g. MyDataLayer.SaveChanges(); db.SaveChanges(); } }