Beispiel #1
0
 public static void SaveImages(AddOrEditProductViewModel vm, int productId)
 {
     if (vm != null && productId > 0)
     {
         SaveImage(vm.CarouselImage, productId, CarouselImageNameFormat);
         SaveImage(vm.DetailsImage, productId, DetailsImageNameFormat);
         SaveImage(vm.PreviewImage, productId, PreviewImageNameFormat);
     }
 }
        public ActionResult Add()
        {
            var vm = new AddOrEditProductViewModel
            {
                Categories = GetCategories()
            };

            return(View(vm));
        }
        public ActionResult AddOrEditProduct(AddOrEditProductViewModel model, HttpPostedFileBase file)
        {
            if (model.Item.ItemId > 0)
            {
                db.Entry(model.Item).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("AddOrEditProduct", new { confirmSuccess = true }));
            }

            else
            {
                // Verify that the user selected a file
                if (file != null && file.ContentLength > 0)
                {
                    if (ModelState.IsValid)
                    { //Generate filename
                        var fileExt = Path.GetExtension(file.FileName);

                        //Generate unique ID for photo
                        var filename = Guid.NewGuid() + fileExt;

                        var path = Path.Combine(Server.MapPath(AppSettings.ItemPhotoPathtoFolder), filename);
                        file.SaveAs(path);

                        //Save info to DB
                        model.Item.ImageFileName   = filename;
                        model.Item.CreateDate      = DateTime.Now;
                        db.Entry(model.Item).State = EntityState.Added;
                        db.SaveChanges();
                        return(RedirectToAction("AddOrEditProduct", new { confirmSuccess = true }));
                    }
                    else
                    {
                        var category = db.Categories.ToArray();
                        model.Category = category;
                        return(View(model));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Nie wskazano pliku.");

                    //download categories form db for DropDownList
                    var category = db.Categories.ToArray();
                    model.Category = category;
                    return(View(model));
                }
            }
        }
        private bool ExtraModelValidation(AddOrEditProductViewModel vm)
        {
            var isModelValid = true;

            // validate images
            var images = new[] { vm.CarouselImage, vm.DetailsImage, vm.PreviewImage };

            if (!images.ToList().TrueForAll(IsValid))
            {
                ModelState.AddModelError("", "You can uplaod png and jpg files only.");
                isModelValid = false;
            }

            return(isModelValid);
        }
        public ActionResult Add(AddOrEditProductViewModel vm)
        {
            if (ModelState.IsValid && ExtraModelValidation(vm))
            {
                var product = Mapper.Map <Product>(vm);
                if (ProductService.Add(product) != 0)
                {
                    MediaHelper.SaveImages(vm, product.Id);
                    return(RedirectToAction("Index", "Dashboard"));
                }

                ModelState.AddModelError("", "An error occurred, product has not been created. Please contact the administrator.");
            }

            vm.Categories = GetCategories();
            return(View(vm));
        }
        public ActionResult AddOrEditProduct(int?itemId, bool?confirmSuccess)
        {
            var result = new AddOrEditProductViewModel();

            if (itemId.HasValue)
            {
                ViewBag.EditMode = true;
                result.Item      = db.Items.Find(itemId);
            }
            else
            {
                ViewBag.EditMode = false;
                result.Item      = new Item();
            }

            //result to send to View
            db.SaveChanges();
            result.Category       = db.Categories.ToList();
            result.ConfirmSuccess = confirmSuccess;
            return(View(result));
        }