//DeleteAddress //This method is in charge of deleting addresses based on the AddressId that the method takes in. public ActionResult DeleteAddress(int id) { //Delete all instances of the AddressID Store.Data.Address A = new Store.Data.Address { AddressID = id }; db.Addresses.Attach(A); db.Addresses.Remove(A); //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 CreateAddressWithUser(Store.Data.Address newAddress, int userID) { if (ModelState.IsValid) { //Populate address object with necessary values newAddress.UserID = userID; newAddress.IsBilling = true; newAddress.IsShipping = true; newAddress.DateCreated = DateTime.Now; newAddress.CreatedBy = "dbo"; newAddress.DateModified = DateTime.Now; newAddress.ModifiedBy = "dbo"; //Add the new address object to the DB and then save the DB db.Addresses.Add(newAddress); db.SaveChanges(); } //Redirect to the method: Address within this controller return(RedirectToAction("Address", "Order")); }
//CreateAddress //Precursor to CreateAddressWithUser. This method will grab the current UserID and will pass the address object and UserID to the next step. public ActionResult CreateAddress(Store.Data.Address newAddress) { int temp = Convert.ToInt32(Session["UserID"].ToString()); return(RedirectToAction("CreateAddressWithUser", "Order", new { newAdress = newAddress, userID = temp })); }