public ActionResult Create(ProductViewModel viewModel) { Product product = new Product(); product.Name = viewModel.Name; product.Description = viewModel.Description; product.Price = viewModel.Price; product.CategoryID = viewModel.CategoryID; product.ProductImageMappings = new List <ProductImageMappings>(); //get a list of selected images without any blanks string[] productImages = viewModel.ProductImages.Where(pi => !string.IsNullOrEmpty(pi)).ToArray(); for (int i = 0; i < productImages.Length; i++) { product.ProductImageMappings.Add(new ProductImageMappings { ProductImage = db.ProductImages.Find(int.Parse(productImages[i])), ImageNumber = i }); } if (ModelState.IsValid) { db.Products.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } viewModel.CategoryList = new SelectList(db.Categories, "ID", "Name", product.CategoryID); viewModel.ImageLists = new List <SelectList>(); for (int i = 0; i < Constants.NumberOfProductImages; i++) { viewModel.ImageLists.Add(new SelectList(db.ProductImages, "ID", "FileName", viewModel.ProductImages[i])); } return(View(viewModel)); }
public ActionResult Create([Bind(Include = "ID,Name")] Category category) { if (ModelState.IsValid) { db.Categories.Add(category); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(category)); }
public ActionResult Create([Bind(Include = "UserID,DeliveryName,DeliveryAddress")] Order order) { if (ModelState.IsValid) { order.DateCreated = DateTime.Now; db.Orders.Add(order); db.SaveChanges(); //add the orderlines to the database after creating the order Basket basket = Basket.GetBasket(); order.TotalPrice = basket.CreateOrderLines(order.OrderID); db.SaveChanges(); return(RedirectToAction("Details", new { id = order.OrderID })); } return(RedirectToAction("Review")); }
public void AddToBasket(int productID, int quantity) { var basketLine = db.BasketLines.FirstOrDefault(b => b.BasketID == BasketID && b.ProductID == productID); if (basketLine == null) { basketLine = new BasketLines { ProductID = productID, BasketID = BasketID, Quantity = quantity, DateCreated = DateTime.Now }; db.BasketLines.Add(basketLine); } else { basketLine.Quantity += quantity; } db.SaveChanges(); }
public ActionResult Upload(HttpPostedFileBase[] files) { bool allValid = true; string inValidFiles = ""; //check the user has entered a file if (files[0] != null) { //if the user has entered less than ten files if (files.Length <= 10) { //check they are all valid foreach (var file in files) { if (!ValidateFile(file)) { allValid = false; inValidFiles += ", " + file.FileName; } } //if they are all valid then try to save them to disk if (allValid) { foreach (var file in files) { try { SaveFileToDisk(file); } catch (Exception) { ModelState.AddModelError("FileName", "Sorry an error occurred saving the files to disk, please try again"); } } } //else add an error listing out the invalid files else { ModelState.AddModelError("FileName", "All files must be gif, png, jpeg or jpg and less than 2MB in size.The following files" + inValidFiles + " are not valid"); } } //the user has entered more than 10 files else { ModelState.AddModelError("FileName", "Please only upload up to ten files at a time"); } } else { //if the user has not entered a file return an error message ModelState.AddModelError("FileName", "Please choose a file"); } if (ModelState.IsValid) { bool duplicates = false; bool otherDbError = false; string duplicateFiles = ""; foreach (var file in files) { //try and save each file var productToAdd = new ProductImage { FileName = file.FileName }; try { db.ProductImages.Add(productToAdd); db.SaveChanges(); } //if there is an exception check if it is caused by a duplicate file catch (DbUpdateException ex) { SqlException innerException = ex.InnerException.InnerException as SqlException; if (innerException != null && innerException.Number == 2601) { duplicateFiles += ", " + file.FileName; duplicates = true; db.Entry(productToAdd).State = EntityState.Detached; } else { otherDbError = true; } } } //add a list of duplicate files to the error message if (duplicates) { ModelState.AddModelError("FileName", "All files uploaded except the files" + duplicateFiles + ", which already exist in the system." + " Please delete them and try again if you wish to re-add them"); return(View()); } else if (otherDbError) { ModelState.AddModelError("FileName", "Sorry an error has occurred saving to the database, please try again"); return(View()); } return(RedirectToAction("Index")); } return(View()); }