public ActionResult Edit(Sale model) { int id = model.Id; int productid = model.ProductId; int customerid = model.CustomerId; int storeid = model.StoreId; DateTime saledate = model.SaleDate; string msg; if (id == 0) { // New Record try { var sale = db.Set <Sale>(); sale.Add(new Sale { Id = id, ProductId = productid, CustomerId = customerid, StoreId = storeid, SaleDate = saledate }); db.SaveChanges(); msg = "New record created."; } catch (Exception) { msg = "Cannot save new record. Contact the administrator."; } } else { // Existing Record var sale = db.Sales.Find(id); if (sale == null) { return(HttpNotFound()); } else { try { sale.ProductId = productid; sale.CustomerId = customerid; sale.SaleDate = saledate; db.SaveChanges(); msg = String.Format("Changes to record {0} have been saved.", id); } catch (Exception) { msg = "Changes cannot be saved. Contact the administrator"; } } } var result = new { success = "True", Message = msg }; return(Json(result, JsonRequestBehavior.AllowGet)); }
public ActionResult Edit(ProductViewModel model) { string msg; int id = model.Id; string name = model.ProductName; decimal?price = model.ProductPrice; if (id == 0) // New Record { try { var newprod = db.Set <Product>(); newprod.Add(new Product { Id = id, ProductName = name, ProductPrice = price }); db.SaveChanges(); msg = "New record has been created"; } catch (Exception) { msg = "Unable to save new record. Contact the administrator."; } } else // Existing record { var editproduct = db.Products.Find(id); if (editproduct == null) { return(HttpNotFound()); } else { try { editproduct.ProductName = name; editproduct.ProductPrice = price; db.SaveChanges(); msg = String.Format("Changes for record {0} have been saved", id); } catch (Exception) { msg = String.Format("Unable to save changes to {0}. Contact the administrator.", id); } } } var result = new { Success = "True", Message = msg }; return(Json(result, JsonRequestBehavior.AllowGet)); }
public ActionResult Edit(StoreViewModel model) { string msg = ""; int id = model.Id; string name = model.StoreName; string address = model.StoreAddress; if (id == 0) { // new record try { var store = db.Set <Store>(); store.Add(new Store { Id = id, StoreName = name, StoreAddress = address }); db.SaveChanges(); msg = "New record has been created."; } catch (Exception) { msg = String.Format("Unable to save changes to record {0}. Contact the administrator", id); } } else { // existing record var editstore = db.Stores.Find(id); if (editstore == null) { return(HttpNotFound()); } try { editstore.StoreName = name; editstore.StoreAddress = address; db.SaveChanges(); msg = string.Format("Changes to record {0} has been saved", id); } catch (Exception) { msg = String.Format("Unable to save changes to record {0}. Contact the administrator.", id); } } var result = new { Success = "true", Message = msg }; return(Json(result, JsonRequestBehavior.AllowGet)); }