public async Task <IActionResult> Create(IFormFile image, IFormCollection form) { if (ModelState.IsValid) { Product product = new Product(); product.Name = form["Product.Name"]; product.Code = form["Product.Code"]; product.Description = form["Product.Description"]; product.Price = Convert.ToDouble(form["Product.Price"]); product.ImgUrl = form["Product.ImgUrl"]; product.ImgName = form["Product.ImgName"]; product.FactoryId = Convert.ToInt32(form["Product.FactoryId"]); product.CategoryId = Convert.ToInt32(form["Product.CategoryId"]); product.ImgName = form["Product.Name"]; string uniqueFileName = null; if (image != null) { string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + image.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { image.CopyTo(fileStream); } product.ImgUrl = uniqueFileName; } _context.Add(product); await _context.SaveChangesAsync(); int id = product.Id; int cnt = Convert.ToInt32(form["UniqueSpecsCtr"]); for (int i = 0; i <= cnt; i++) { var uniqueSpecName = form["ProductUniqueSpec[" + i + "].Name"]; var uniqueSpecValue = form["ProductUniqueSpec[" + i + "].Value"]; if (uniqueSpecName.Count > 0 && uniqueSpecValue.Count > 0) { ProductUniqueSpec tmp = new ProductUniqueSpec(); tmp.Name = uniqueSpecName; tmp.Value = uniqueSpecValue; tmp.ProductId = id; _context.Add(tmp); } } await _context.SaveChangesAsync(); cnt = Convert.ToInt32(form["CategorySpecsCtr"]); int ndx = 0; for (int i = 0; i <= cnt; i++) { var categorySpecValue = form["ProductSpec[" + i + "].Value"]; if (categorySpecValue.Count > 0) { var categorySpecId = form["CategorySpec.Id"][ndx++]; ProductSpec tmp = new ProductSpec(); tmp.CategorySpecId = Convert.ToInt32(categorySpecId); tmp.Value = categorySpecValue; tmp.ProductId = id; _context.Add(tmp); } } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View()); }
public async Task <IActionResult> Edit(IFormCollection form) { int productId = Convert.ToInt32(form["Product.Id"]); var product = _context.Products.Where(p => p.Id == productId).FirstOrDefault(); if (product == null) { return(NotFound()); } if (ModelState.IsValid) { //Product product = new Product(); int oldCategoryId = product.CategoryId; product.Id = productId; product.Name = form["Product.Name"]; product.Code = form["Product.Code"]; product.Description = form["Product.Description"]; product.Price = Convert.ToDouble(form["Product.Price"]); product.ImgUrl = form["Product.ImgUrl"]; product.ImgName = form["Product.ImgName"]; product.FactoryId = Convert.ToInt32(form["Product.FactoryId"]); product.CategoryId = Convert.ToInt32(form["Product.CategoryId"]); int cnt = 0; if (product.CategoryId == oldCategoryId) { var tmpCtr = form["CategorySpecs.Count"]; if (tmpCtr.Count == 0) { tmpCtr = form["CategorySpecsCtr"]; } cnt = Convert.ToInt32(tmpCtr); int ndx = 0; for (int i = 0; i <= cnt; i++) { var categorySpecValue = form["ProductSpec[" + i + "].Value"]; if (categorySpecValue.Count > 0) { var tmpCategorySpecId = form["CategorySpec.Id"][ndx++]; var categorySpecId = 0; if (tmpCategorySpecId != null && tmpCategorySpecId != "") { categorySpecId = Convert.ToInt32(tmpCategorySpecId); var ProductSpec = _context.ProductSpecs.Where(ps => ps.ProductId == productId && ps.CategorySpecId == categorySpecId).FirstOrDefault(); ProductSpec.Value = form["ProductSpec[" + i + "].Value"]; _context.Update(ProductSpec); } } } } else { var ProductSpecs = _context.ProductSpecs.Where(ps => ps.ProductId == productId).ToList(); foreach (var productSpec in ProductSpecs) { _context.ProductSpecs.Remove(productSpec); } cnt = Convert.ToInt32(form["CategorySpecsCtr"]); int ndx = 0; for (int i = 0; i <= cnt; i++) { var categorySpecValue = form["ProductSpec[" + i + "].Value"]; if (categorySpecValue.Count > 0) { var categorySpecId = form["CategorySpec.Id"][ndx++]; ProductSpec ProductSpec = new ProductSpec(); ProductSpec.CategorySpecId = Convert.ToInt32(categorySpecId); ProductSpec.Value = categorySpecValue; ProductSpec.ProductId = productId; _context.Update(ProductSpec); } } } var OldUniqueSpecs = _context.ProductUniqueSpecs.Where(us => us.ProductId == productId).ToList(); cnt = OldUniqueSpecs.Count; foreach (var oldUniqueSpec in OldUniqueSpecs) { bool flag = false; for (int i = 0; i <= cnt; i++) { var tmp = form["OldProductUniqueSpec[" + i + "].Id"]; if (tmp.Count > 0) { int tmpId = Convert.ToInt32(tmp); if (tmpId == oldUniqueSpec.Id) { oldUniqueSpec.Name = form["OldProductUniqueSpec[" + i + "].Name"]; oldUniqueSpec.Value = form["OldProductUniqueSpec[" + i + "].Value"]; _context.Update(oldUniqueSpec); flag = true; break; } } } if (!flag) { _context.ProductUniqueSpecs.Remove(oldUniqueSpec); } } cnt = Convert.ToInt32(form["UniqueSpecsCtr"]); for (int i = 0; i <= cnt; i++) { var uniqueSpecName = form["ProductUniqueSpec[" + i + "].Name"]; var uniqueSpecValue = form["ProductUniqueSpec[" + i + "].Value"]; if (uniqueSpecName.Count > 0 && uniqueSpecValue.Count > 0) { ProductUniqueSpec tmp = new ProductUniqueSpec(); tmp.Name = uniqueSpecName; tmp.Value = uniqueSpecValue; tmp.ProductId = productId; _context.Add(tmp); } } _context.Update(product); await _context.SaveChangesAsync(); } return(RedirectToAction("Index")); }