public void addProductForm_InsertItem() { var item = new FrontierAg.Models.Product(); TryUpdateModel(item); if (ModelState.IsValid) { // Save changes here using (FrontierAg.Models.ProductContext db = new FrontierAg.Models.ProductContext()) { item.DateCreated = DateTime.Now; db.Products.Add(item); db.SaveChanges(); } } }
// The id parameter name should match the DataKeyNames value set on the control public void ProductsGrid_UpdateItem(int ProductId) { FrontierAg.Models.Product item = null; item = _db.Products.Find(ProductId); // Load the item here, e.g. item = MyDataLayer.Find(id); if (item == null) { // The item wasn't found ModelState.AddModelError("", String.Format("Item with id {0} was not found", ProductId)); return; } TryUpdateModel(item); if (ModelState.IsValid) { // Save changes here, e.g. MyDataLayer.SaveChanges(); _db.SaveChanges(); } }