public void addVehicleForm_InsertItem() { var item = new Models.Product(); TryUpdateModel(item); if (ModelState.IsValid) { using (ProductContext db = new ProductContext()) { db.Products.Add(item); db.SaveChanges(); } } }
// The id parameter name should match the DataKeyNames value set on the control public void productsGrid_DeleteItem(int productID) { using (ProductContext db = new ProductContext()) { var item = new Product { ProductID = productID }; db.Entry(item).State = System.Data.Entity.EntityState.Deleted; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { ModelState.AddModelError("",String.Format("Item with id {0} no longer exists in the database.", productID)); } } }