Example #1
0
        public ActionResult Create(int categoryId, FormCollection form)
        {
            try
            {
                using (var context = new SiteContainer())
                {
                    var category = context.Category.First(c => c.Id == categoryId);
                    var product = new Product { Category = category, ImageSource = "" };
                    TryUpdateModel(product, new[] { "Title", "Discount", "Price", "Structure", "Consistence", "Producer", "Nap", "PageTitle" });

                    product.Description = HttpUtility.HtmlDecode(form["Description"]);
                    product.DiscountText = HttpUtility.HtmlDecode(form["DiscountText"]);

                    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);
                        GraphicsHelper.SaveOriginalImage(filePath, fileName, file, 1500);
                        //file.SaveAs(filePath);

                        pi.ImageSource = fileName;
                        product.ProductImages.Add(pi);
                        if (string.IsNullOrEmpty(product.ImageSource))
                            product.ImageSource = pi.ImageSource;
                    }

                    context.AddToProduct(product);
                    context.SaveChanges();

                    if (category.SpecialCategory)
                    {
                        return RedirectToAction("OurWorks", "Home", new { area = "" });
                    }

                    return RedirectToAction("Gallery", "Home", new { area = "", id = category.Name });
                }
            }
            catch (Exception ex)
            {
                return View(ViewBag.ErrorMessage = ex.Message);
            }
        }
Example #2
0
        public ActionResult Create(int categoryId, FormCollection form, HttpPostedFileBase fileUpload)
        {
            try
            {
                using (var context = new SiteContainer())
                {
                    var category = context.Category.First(c => c.Id == categoryId);
                    var product = new Product{Category = category};
                    TryUpdateModel(product, new[] {"Title", "Discount", "DiscountText", "Price"});

                    product.Description = HttpUtility.HtmlDecode(form["Description"]);
                    if (fileUpload != null)
                    {
                        //if (!string.IsNullOrEmpty(product.ImageSource))
                        //{

                        //    IOHelper.DeleteFile("~/Content/Images", product.ImageSource);
                        //    foreach (var thumbnail in SiteSettings.Thumbnails)
                        //    {
                        //        IOHelper.DeleteFile("~/ImageCache/" + thumbnail.Key, product.ImageSource);
                        //    }
                        //}
                        string fileName = IOHelper.GetUniqueFileName("~/Content/Images", fileUpload.FileName);
                        string filePath = Server.MapPath("~/Content/Images");
                        filePath = Path.Combine(filePath, fileName);
                        fileUpload.SaveAs(filePath);
                        product.ImageSource = fileName;
                    }

                    context.AddToProduct(product);

                    context.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }