public class CategoriesController : Controller
{
private readonly ApplicationDbContext _context;
private readonly IWebHostEnvironment _hostEnvironment;
public CategoriesController(ApplicationDbContext context, IWebHostEnvironment hostEnvironment)
{
_context = context;
_hostEnvironment = hostEnvironment;
}
// GET: Categories
public async Task<IActionResult> Index()
{
return View(await _context.Categories.ToListAsync());
}
// GET: Categories/Create
public IActionResult Create()
{
return View();
}
// POST: Categories/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Category category, IFormFile pic)
{
if (ModelState.IsValid)
{
if (pic != null && pic.Length > 0)
{
string fileName = Path.GetFileName(pic.FileName);
string extension = Path.GetExtension(pic.FileName).ToLower();
// Check if file is a valid image
if (extension == ".jpg" || extension == ".png" || extension == ".bmp" || extension == ".jpeg" || extension == ".gif")
{
string filePath = Path.Combine(_hostEnvironment.WebRootPath, "DataFiles", fileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await pic.CopyToAsync(stream);
}
category.CategoryImage = fileName;
}
}
_context.Add(category);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(category);
}
// GET: Categories/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var category = await _context.Categories.FindAsync(id);
if (category == null)
{
return NotFound();
}
return View(category);
}
// POST: Categories/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, Category category, IFormFile pic)
{
if (id != category.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
if (pic != null && pic.Length > 0)
{
string fileName = Path.GetFileName(pic.FileName);
string extension = Path.GetExtension(pic.FileName).ToLower();
if (extension == ".jpg" || extension == ".png" || extension == ".bmp" || extension == ".jpeg" || extension == ".gif")
{
string filePath = Path.Combine(_hostEnvironment.WebRootPath, "DataFiles", fileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await pic.CopyToAsync(stream);
}
category.CategoryImage = fileName;
}
}
_context.Update(category);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!CategoryExists(category.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(category);
}
// GET: Categories/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var category = await _context.Categories
.FirstOrDefaultAsync(m => m.Id == id);
if (category == null)
{
return NotFound();
}
return View(category);
}
// POST: Categories/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var category = await _context.Categories.FindAsync(id);
// Delete the image file
if (category.CategoryImage != null)
{
var imagePath = Path.Combine(_hostEnvironment.WebRootPath, "DataFiles", category.CategoryImage);
if (System.IO.File.Exists(imagePath))
{
System.IO.File.Delete(imagePath);
}
}
_context.Categories.Remove(category);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool CategoryExists(int id)
{
return _context.Categories.Any(e => e.Id == id);
}
}
https://www.c-sharpcorner.com/article/asp-net-mvc5-full-calendar-jquery-plugin/
ReplyDelete