public ActionResult Edit(int id, string product_name, string product_price, FormCollection form, List <int> product_count, List <int> product_size_count, List <int> product_pivot_id) { Product pr = db.Products.Find(id); pr.product_name = product_name; pr.product_price = product_price; db.SaveChanges(); List <ProductImage> productimg = db.ProductImages.Where(s => s.product_img_product_id == id).ToList(); if (product_count != null) { for (int i = 0; i < productimg.Count; i++) { productimg[i].product_img_count = product_count[i]; db.SaveChanges(); } } else { for (int i = 0; i < product_pivot_id.Count; i++) { ProductPivotSize pivot = db.ProductPivotSizes.Find(product_pivot_id[i]); pivot.product_pivot_count = product_size_count[i]; db.SaveChanges(); } } return(RedirectToAction("Index")); }
public ActionResult Create(Product product, List <HttpPostedFileBase> file, List <int> colorId, FormCollection form, List <int> product_size_count, List <int> product_count) { List <string> size_str_id = form["checkBoxList_name"].Split(',').ToList(); List <List <int> > size_id = new List <List <int> >(); foreach (string item in size_str_id) { List <int> list = new List <int>(); foreach (char c in item) { list.Add((int)Char.GetNumericValue(c)); } size_id.Add(list); } ProductPivotSize new_productsize = new ProductPivotSize(); Random rnd = new Random(); var randonnumber = rnd.Next(0, 100000); int product_business = LoginRegisterController.log_business.business_id; product.product_business_id = product_business; db.Products.Add(product); db.SaveChanges(); var productLast = db.Products.OrderByDescending(a => a.product_id).FirstOrDefault(); for (int i = 0; i < file.Count(); i++) { if (file[i] != null && colorId[i] != 0) { var InputFileName = Path.GetFileName(file[i].FileName); var ServerSavePath = Path.Combine(Server.MapPath("~/Upload/") + randonnumber + InputFileName); //Save file to server folder file[i].SaveAs(ServerSavePath); ProductImage productImage = new ProductImage(); productImage.product_img = "/Upload/" + randonnumber + InputFileName; productImage.product_img_color_id = colorId[i]; productImage.product_img_product_id = productLast.product_id; if (product_count[i] > 0) { productImage.product_img_count = Convert.ToInt32(product_count[i]); } else { productImage.product_img_count = null; } db.ProductImages.Add(productImage); db.SaveChanges(); for (int s = 0; s < size_id[i].Count; s++) { ProductPivotSize elem = new ProductPivotSize(); elem.product_pivot_img_id = productImage.product_img_id; elem.product_pivot_size_id = size_id[i][s]; elem.product_pivot_count = 5; db.ProductPivotSizes.Add(elem); db.SaveChanges(); } } } return(RedirectToAction("Index")); }