public async Task <IActionResult> Edit(IFormFile file, Slider slider) { if (ModelState.IsValid) { try { if (file != null) { if (slider.ImageCode != null && slider.ImageCode != string.Empty) { ImagesHelpers.DeleteImage(slider.ImageCode, "img/slider/"); } slider.ImageCode = await ImagesHelpers.ImageUploadAsync(_env.WebRootPath, file, "img", "slider"); } _context.Update(slider); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SliderExists(slider.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(slider)); }
public async Task <IActionResult> Edit(IFormFile file, Discountend discountet) { if (ModelState.IsValid) { try { if (file != null) { if (discountet.ImageCod != null && discountet.ImageCod != string.Empty) { ImagesHelpers.DeleteImage(discountet.ImageCod, "img/discountet/"); } discountet.ImageCod = await ImagesHelpers.ImageUploadAsync(_env.WebRootPath, file, "img", "discountet"); } _context.Update(discountet); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!DiscountetExists(discountet.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(discountet)); }
public async Task <IActionResult> Edit(IFormFile file, int id, Category cate) { if (id != cate.Id) { return(NotFound()); } if (!ModelState.IsValid) { return(View(cate)); } if (file != null) { if (cate.ImageCode != null && cate.ImageCode != string.Empty) { ImagesHelpers.DeleteImage(cate.ImageCode, "img/category/"); } cate.ImageCode = await ImagesHelpers.ImageUploadAsync(_env.WebRootPath, file, "img", "category"); } _context.Update(cate); await _context.SaveChangesAsync(); var ProCategory = await _context.Categories.FindAsync(id); ProCategory.Name = cate.Name; await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Edit(IFormFile file, int id, About about) { if (id != about.Id) { return(NotFound()); } if (!ModelState.IsValid) { return(View(about)); } if (file != null) { if (about.ImageCode != null && about.ImageCode != string.Empty) { ImagesHelpers.DeleteImage(about.ImageCode, "img/about/"); } about.ImageCode = await ImagesHelpers.ImageUploadAsync(_env.WebRootPath, file, "img", "about"); } _context.Update(about); await _context.SaveChangesAsync(); var ProAbout = await _context.Abouts.FindAsync(id); ProAbout.Title = about.Title; await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Create(IFormFile file, Discountend discountet) { if (ModelState.IsValid) { if (file != null) { discountet.ImageCod = await ImagesHelpers.ImageUploadAsync(_env.WebRootPath, file, "img", "discountet"); } _context.Add(discountet); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(discountet)); }
public async Task <IActionResult> Create(IFormFile file, Category cate) { if (ModelState.IsValid) { if (file != null) { cate.ImageCode = await ImagesHelpers.ImageUploadAsync(_env.WebRootPath, file, "img", "category"); } var result = await _context.Categories.AddAsync(cate); await _context.SaveChangesAsync(); } return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Create(IFormFile file, Slider slider) { if (ModelState.IsValid) { if (file != null) { slider.ImageCode = await ImagesHelpers.ImageUploadAsync(_env.WebRootPath, file, "img", "slider"); } _context.Add(slider); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(slider)); }
public async Task <IActionResult> Edit(ViewModel viewModel, List <IFormFile> file) { if (ModelState.IsValid) { Product product = await _context.Products.FindAsync(viewModel.Product.Id); if (file.Count != 0) { List <ProductImage> images = await _context.ProductImages.Where(i => i.ProductId == viewModel.Product.Id).ToListAsync(); foreach (var item in images) { if (item.ImageCode != null && item.ImageCode != string.Empty) { ImagesHelpers.DeleteImage(item.ImageCode, "img/product/"); } } _context.ProductImages.RemoveRange(images); } foreach (var item in file) { var image = await ImagesHelpers.ImageUploadAsync(_env.WebRootPath, item, "img", "product"); await _context.ProductImages.AddAsync(new ProductImage { ProductId = viewModel.Product.Id, ImageCode = image }); await _context.SaveChangesAsync(); } { product.Name = viewModel.Product.Name; product.Description = viewModel.Product.Description; product.CategoryId = viewModel.Category.Id; product.Color = viewModel.ProductColor; product.Size = viewModel.ProductSize; product.DiscountPrice = viewModel.Product.DiscountPrice; product.Price = viewModel.Product.Price; await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } return(View(viewModel)); }
public async Task <IActionResult> Edit(ViewModel viewModel, IFormFile file) { if (ModelState.IsValid) { Blog blog = await _context.Blogs.FindAsync(viewModel.Blog.Id); if (file != null) { List <BlogImage> images = await _context.BlogImages.Where(i => i.BlogId == viewModel.Blog.Id).ToListAsync(); foreach (var item in images) { if (item.ImageCode != null && item.ImageCode != string.Empty) { ImagesHelpers.DeleteImage(item.ImageCode, "img/blog/"); } } _context.BlogImages.RemoveRange(images); } var image = await ImagesHelpers.ImageUploadAsync(_env.WebRootPath, file, "img", "blog"); await _context.BlogImages.AddAsync(new BlogImage { BlogId = viewModel.Blog.Id, ImageCode = image }); await _context.SaveChangesAsync(); { blog.Title = viewModel.Blog.Title; blog.Description = viewModel.Blog.Description; blog.DateTime = viewModel.Blog.DateTime; blog.UserName = viewModel.Blog.UserName; await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } return(View(viewModel)); }
public async Task <IActionResult> Create(ViewModel viewModel, List <IFormFile> files) { if (ModelState.IsValid) { var Catagory = await _context.Categories.FindAsync(viewModel.Category.Id); Product model = new Product() { Name = viewModel.Product.Name, Description = viewModel.Product.Description, CategoryId = Catagory.Id, Color = viewModel.ProductColor, Size = viewModel.ProductSize, DiscountPrice = viewModel.Product.DiscountPrice, Price = viewModel.Product.Price, }; await _context.Products.AddAsync(model); await _context.SaveChangesAsync(); int count = 0; foreach (var item in files) { var image = await ImagesHelpers.ImageUploadAsync(_env.WebRootPath, item, "img", "product"); await _context.ProductImages.AddAsync(new ProductImage { ProductId = model.Id, ImageCode = image }); if (count < files.Count()) { model.ProductImages[count].ImageCode = image; ++count; } await _context.SaveChangesAsync(); } } return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Create(Blog blog, IFormFile file) { if (ModelState.IsValid) { var ProBlog = await _context.Blogs.AddAsync(blog); await _context.SaveChangesAsync(); if (file != null) { var image = await ImagesHelpers.ImageUploadAsync(_env.WebRootPath, file, "img", "blog"); await _context.BlogImages.AddAsync(new BlogImage { BlogId = blog.Id, ImageCode = image }); } await _context.SaveChangesAsync(); } return(RedirectToAction(nameof(Index))); }