public ActionResult Create(ProductImage model) { _repository.LangId = CurrentLangId; var productId = model.ProductId; try { var product = _repository.GetProduct(productId); for (int i = 0; i < Request.Files.Count; i++) { var file = Request.Files[i]; if (file == null) continue; if (string.IsNullOrEmpty(file.FileName)) continue; string fileName = IOHelper.GetUniqueFileName("~/Content/Images", file.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); GraphicsHelper.SaveOriginalImage(filePath, fileName, file, 1500); var productImage = new ProductImage { ImageSource = fileName }; product.ProductImages.Add(productImage); } _repository.SaveProduct(product); } catch (Exception ex) { TempData["errorMessage"] = ex.Message; return View(); } return RedirectToAction("Index", new {id = productId}); }
public void SaveProductImage(ProductImage productImage) { _store.SaveChanges(); }
public ActionResult Create(Product model, FormCollection form) { _repository.LangId = CurrentLangId; try { model.Id = 0; var categories = _repository.GetCategories(); int categoryId = int.Parse(form["categoryId"]); foreach (var category in categories.Where(category => category.Id == categoryId)) { category.Selected = true; } ViewBag.Categories = categories; var product = new Product { Name = string.IsNullOrEmpty(model.Name) ? SiteHelper.UpdatePageWebName(model.Name, model.Title) : SiteHelper.UpdatePageWebName(model.Name), CategoryId = categoryId, Title = model.Title, SeoDescription = model.SeoDescription, SeoKeywords = model.SeoKeywords, SeoText = model.SeoText, IsActive = model.IsActive, IsDiscount = model.IsDiscount, IsNew = model.IsNew, IsTopSale = model.IsTopSale, OldPrice = model.OldPrice, Price = model.Price, SearchCriteriaAttributes = "", ExternalId = model.ExternalId }; product.Description = model.Description == null ? "" : HttpUtility.HtmlDecode(model.Description); for (int i = 0; i < Request.Files.Count; i++) { var file = Request.Files[i]; if (file == null) continue; if (string.IsNullOrEmpty(file.FileName)) continue; string fileName = IOHelper.GetUniqueFileName("~/Content/Images", file.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); GraphicsHelper.SaveOriginalImage(filePath, fileName, file, 1500); var productImage = new ProductImage { ImageSource = fileName }; product.ProductImages.Add(productImage); } _repository.AddProduct(product); } catch (Exception ex) { TempData["errorMessage"] = ex.Message; return View(model); } return RedirectToAction("Index"); }