public ActionResult Remove(int id) { //Delete all instances of the ShoppingCartProductID with this method Store.Data.ShoppingCartProduct S = new Store.Data.ShoppingCartProduct { ShoppingCartProductID = id }; db.ShoppingCartProducts.Attach(S); db.ShoppingCartProducts.Remove(S); //Try saving try { db.SaveChanges(); } //If saving does not work catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); // raise a new exception nesting // the current instance as InnerException raise = new InvalidOperationException(message, raise); } } throw raise; } //Pass the ShoppingCartID back to the Ajax function return(Json(new { newID = id })); }
public ActionResult TestOrderNow(int?id, int userID) { //Checks if any cart exists if (db.ShoppingCarts.FirstOrDefault(t => t.ShoppingCartID == userID) == null) { //If not, create a new one with parameters Store.Data.ShoppingCart S = new Store.Data.ShoppingCart(); S.ShoppingCartID = userID; S.UserID = userID; S.CreatedBy = "dbo"; DateTime date1 = DateTime.Now; S.DateCreated = date1; S.ModifiedBy = "dbo"; S.DateModified = date1; //Add the new ShoppingCart to the DB db.ShoppingCarts.Add(S); //Try saving try { db.SaveChanges(); } //If saving does not work catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); // raise a new exception nesting // the current instance as InnerException raise = new InvalidOperationException(message, raise); } } throw raise; } } //check if product exists in the user's cart if (db.ShoppingCartProducts.FirstOrDefault(t => t.ShoppingCartID == userID && t.ProductID == id) == null) { //If the product does not exist, then add it to the shopping cart Store.Data.ShoppingCartProduct Shop = new Store.Data.ShoppingCartProduct(); Shop.ShoppingCartID = userID; Shop.ProductID = (int)id; Shop.Quantity = 1; Shop.CreatedBy = "dbo"; DateTime date1 = DateTime.Now; Shop.DateCreated = date1; Shop.ModifiedBy = "dbo"; Shop.DateModified = date1; //Add the new ShoppingCartProduct to the DB db.ShoppingCartProducts.Add(Shop); } //If the Product already exists in the Shopping Cart, simply increase the quantity else { db.ShoppingCartProducts.FirstOrDefault(t => t.ProductID == id && t.ShoppingCartID == userID).Quantity++; } //Try saving try { db.SaveChanges(); } //If saving does not work catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); // raise a new exception nesting // the current instance as InnerException raise = new InvalidOperationException(message, raise); } } throw raise; } //Redirect to the ShoppingCart view return(Redirect("~/ShoppingCart/Cart")); }