public ActionResult Create(FormCollection form, int categoryId, HttpPostedFileBase fileUpload) { using (var context = new StructureContainer()) { Product product = new Product(); Category category = context.Category.Include("Parent").First(c => c.Id == categoryId); product.Category = category; var attributes = context.ProductAttribute.ToList(); PostCheckboxesData postData = form.ProcessPostCheckboxesData("attr", "categoryId"); foreach (var kvp in postData) { var attribute = attributes.First(a => a.Id == kvp.Key); if (kvp.Value) { if (!product.ProductAttributes.Contains(attribute)) product.ProductAttributes.Add(attribute); } else { if (product.ProductAttributes.Contains(attribute)) product.ProductAttributes.Remove(attribute); } } TryUpdateModel(product, new[] { "Title", "TitleEng", "Description", "DescriptionEng", "ShowOnMainPage","Discount","DiscountText" }); if (fileUpload != null) { string fileName = IOHelper.GetUniqueFileName("~/Content/Images", fileUpload.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); GraphicsHelper.SaveOriginalImage(filePath, fileName, fileUpload); product.ImageSource = fileName; } context.AddToProduct(product); context.SaveChanges(); return category.Parent != null ? RedirectToAction("Index", "Catalogue", new { Area = "", category = category.Parent.Name, subCategory = category.Name }) : RedirectToAction("Index", "Catalogue", new { Area = "", category = category.Name }); } }
public ActionResult CreateMany(FormCollection form, int categoryId, IList<HttpPostedFileBase> fileUpload, IList<string> titleRU, IList<string> titleEN, IList<string> descriptionRU, IList<string> descriptionEN) { using (var context = new StructureContainer()) { Category category = context.Category.Include("Parent").First(c => c.Id == categoryId); var attributes = context.ProductAttribute.ToList(); PostCheckboxesData postData = form.ProcessPostCheckboxesData("attr", "categoryId"); for (int i = 0; i < 10; i++) { if (fileUpload[i] != null) { Product product = new Product {Category = category}; string fileName = IOHelper.GetUniqueFileName("~/Content/Images", fileUpload[i].FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); GraphicsHelper.SaveOriginalImage(filePath, fileName, fileUpload[i]); product.ImageSource = fileName; foreach (var kvp in postData) { var attribute = attributes.First(a => a.Id == kvp.Key); if (kvp.Value) { if (!product.ProductAttributes.Contains(attribute)) product.ProductAttributes.Add(attribute); } else { if (product.ProductAttributes.Contains(attribute)) product.ProductAttributes.Remove(attribute); } } product.Title = titleRU[i]; product.TitleEng = titleEN[i]; product.Description = descriptionRU[i]; product.DescriptionEng = descriptionEN[i]; context.AddToProduct(product); } } context.SaveChanges(); return category.Parent != null ? RedirectToAction("Index", "Catalogue", new { Area = "", category = category.Parent.Name, subCategory = category.Name }) : RedirectToAction("Index", "Catalogue", new { Area = "", category = category.Name }); } }