public void addPackChargeForm_InsertItem() { var item = new FrontierAg.Models.PackCharge(); TryUpdateModel(item); if (ModelState.IsValid) { // Save changes here using (FrontierAg.Models.ProductContext db = new FrontierAg.Models.ProductContext()) { db.PackCharges.Add(item); db.SaveChanges(); } } }
// The id parameter name should match the DataKeyNames value set on the control public void PackChargesGrid_UpdateItem(int PackChargeId) { FrontierAg.Models.PackCharge item = null; item = _db.PackCharges.Find(PackChargeId); // 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", PackChargeId)); return; } TryUpdateModel(item); if (ModelState.IsValid) { // Save changes here, e.g. MyDataLayer.SaveChanges(); _db.SaveChanges(); } }