Esempio n. 1
0
        public async Task <ActionResult> Create(ProductView view) //recibe un productview en vez de un product
        {
            if (ModelState.IsValid)
            {
                var pic    = string.Empty;
                var folder = "~/Content/Products";

                if (view.ImageFile != null)//el usuario subio una imagen
                {
                    pic = FilesHepler.UploadPhoto(view.ImageFile, folder);
                    pic = $"{folder}/{pic}";
                }

                var product = this.ToProduct(view, pic); //transforma el ProductView a product



                db.Products.Add(product);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(view));
        }
Esempio n. 2
0
        public async Task <ActionResult> Create(CategoryView view)
        {
            if (ModelState.IsValid)
            {
                var pic    = string.Empty;
                var folder = "~/Content/Categories";

                if (view.ImageFile != null)
                {
                    pic = FilesHepler.UploadPhoto(view.ImageFile, folder);
                    pic = $"{folder}/{pic}";
                }

                var category = this.ToCategory(view, pic);
                this.db.Categories.Add(category);
                await this.db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(view));
        }
Esempio n. 3
0
        public async Task <ActionResult> Edit(CategoryView view)
        {
            if (ModelState.IsValid)
            {
                var pic    = view.ImagePath;
                var folder = "~/Content/Categories";

                if (view.ImageFile != null)
                {
                    pic = FilesHepler.UploadPhoto(view.ImageFile, folder);
                    pic = $"{folder}/{pic}";
                }

                var category = this.ToCategory(view, pic);
                this.db.Entry(category).State = EntityState.Modified;
                await this.db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(view));
        }