Exemplo n.º 1
0
        public ActionResult Create(Product model)
        {
            try
            {
                model.Text = HttpUtility.HtmlDecode(model.Text);
                model.Id = 0;
                var category = _context.Categories.First(c => c.Id == model.CategoryId);
                var cache = new Product
                {
                    Name = SiteHelper.UpdatePageWebName(model.Name),
                    SortOrder = model.SortOrder,
                    Category = category,
                    Title = model.Title,
                    Text = model.Text,
                    IsContentPage = model.IsContentPage,
                    
                };

                for (int i = 0; i < Request.Files.Count; i++)
                {
                    var file = Request.Files[i];
                    if (file == null) continue;
                    if (string.IsNullOrEmpty(file.FileName)) continue;
                    var pi = new ProductImage();
                    string fileName = IOHelper.GetUniqueFileName("~/Content/Images", file.FileName);
                    string filePath = Server.MapPath("~/Content/Images");

                    filePath = Path.Combine(filePath, fileName);
                    //file.SaveAs(filePath);
                    GraphicsHelper.SaveOriginalImage(filePath, fileName, file);
                    pi.ImageSource = fileName;
                    cache.ProductImages.Add(pi);
                }


                _context.Products.Add(cache);

                var lang = _context.Languages.FirstOrDefault(p => p.Id == model.CurrentLang);
                if (lang != null)
                {
                    CreateOrChangeContentLang(_context, model, cache, lang);
                }

                return RedirectToAction("Details", "Category", new { id = category.Id });
            }
            catch
            {
                return View(model);
            }
        }
Exemplo n.º 2
0
        public ActionResult AddImage(int productId, HttpPostedFileBase fileUpload)
        {
            var product = _context.Products.First(p => p.Id == productId);
            if (fileUpload != null)
            {
                string fileName = IOHelper.GetUniqueFileName("~/Content/Images", fileUpload.FileName);
                string filePath = Server.MapPath("~/Content/Images");
                filePath = Path.Combine(filePath, fileName);
                fileUpload.SaveAs(filePath);
                var pi = new ProductImage
                {
                    ImageSource = fileName
                };
                product.ProductImages.Add(pi);
                _context.SaveChanges();
            }

            return RedirectToAction("Index", "Category", new {area = "Admin"});
        }