Exemplo n.º 1
0
        public ActionResult Add()
        {
            IEnumerable <AllCategoriesVm> categories = this.service.GetAllCategoriesTitles();
            AddProcuctVm vm = new AddProcuctVm()
            {
                Categories = categories
            };

            return(this.View(vm));
        }
Exemplo n.º 2
0
        public ActionResult Add(AddProductBm bm, HttpPostedFileBase image)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(bm));
            }
            try
            {
                string imagePath = "";
                if (image != null)
                {
                    var allowedTypes = new[] { "image/jpeg", "image/jpg", "image/png" };
                    if (allowedTypes.Contains(image.ContentType))
                    {
                        var imagesPath = "/Content/Images/";

                        var fileName = image.FileName;

                        var uploadPath = imagesPath + fileName;

                        var psihicalPath = Server.MapPath(uploadPath);

                        image.SaveAs(psihicalPath);

                        bm.ImageUrl = uploadPath;
                    }
                }
                this.service.AddProduct(bm);
                return(RedirectToAction("all"));
            }
            catch (DbEntityValidationException ex)
            {
                var error = ex.EntityValidationErrors.First().ValidationErrors.First();
                this.ModelState.AddModelError(error.PropertyName, error.ErrorMessage);

                IEnumerable <AllCategoriesVm> categories = this.service.GetAllCategoriesTitles();
                AddProcuctVm vm = new AddProcuctVm()
                {
                    Categories = categories
                };

                return(View(vm));
            }
        }