Exemplo n.º 1
0
        public async Task <IActionResult> Edit(Category category)
        {
            if (!ModelState.IsValid)
            {
                return(View(category));
            }
            var categoryDb = await _context.Categories.FindAsync(category.Id);

            if (category.CategoryPhoto != null)
            {
                //new photo exists, so delete old one, save new one
                try
                {
                    var newPhoto = await category.CategoryPhoto.SaveFileAsync(_env.WebRootPath, "img/feature");

                    IFormFileExtensions.Delete(_env.WebRootPath, "img/feature", categoryDb.CategoryImage);

                    categoryDb.CategoryImage = newPhoto;
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", "Unexpected error happened while saving image. Please, try again.");
                    return(View(category));
                }
            }

            categoryDb.Name = category.Name;

            await _context.SaveChangesAsync();

            //TempData["success_message"] = "Category was updated successfully";

            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> DeletePost(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Category category = await _context.Categories.FindAsync(id);

            if (category == null)
            {
                return(NotFound());
            }


            IFormFileExtensions.Delete(_env.WebRootPath, "img/feature", category.CategoryImage);
            _context.Categories.Remove(category);
            await _context.SaveChangesAsync();

            //TempData["success_message"] = "Category was removed successfully";

            return(RedirectToAction(nameof(Index)));
        }