public void ListViewPosts_InsertItem() { var post = new ChatCanal.Models.Post(); TryUpdateModel(post); if (ModelState.IsValid) { ApplicationDbContext context = new ApplicationDbContext(); context.Posts.Add(post); context.SaveChanges(); } }
// The id parameter name should match the DataKeyNames value set on the control public void ListViewPosts_UpdateItem(int id) { ChatCanal.Models.Post post = null; ApplicationDbContext context = new ApplicationDbContext(); post = context.Posts.Find(id); // Load the item here, e.g. item = MyDataLayer.Find(id); if (post == null) { // The item wasn't found ModelState.AddModelError("", String.Format("Item with id {0} was not found", id)); return; } TryUpdateModel(post); if (ModelState.IsValid) { context.SaveChanges(); } }