Dodawanie/Usuwanie

Dodawanie/Usuwanie (nie chce tu przeklinać)
This commit is contained in:
Witkopawel
2025-04-25 05:19:36 +02:00
parent 981236f6aa
commit 9151fbb6ab
6 changed files with 163 additions and 24 deletions

View File

@@ -0,0 +1,50 @@
using Microsoft.AspNetCore.Mvc;
using WebApp.Data;
using WebApp.Entities;
namespace WebApp.Controllers
{
public class EventsController : Controller
{
private readonly ApplicationDbContext _context;
public EventsController(ApplicationDbContext context)
{
_context = context;
}
public IActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Event ev)
{
if (ModelState.IsValid)
{
ev.Date = DateTime.SpecifyKind(ev.Date, DateTimeKind.Utc);
_context.Events.Add(ev);
await _context.SaveChangesAsync();
return RedirectToAction("Index", "Home");
}
return View(ev);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Delete(int id)
{
var ev = await _context.Events.FindAsync(id);
if (ev != null)
{
_context.Events.Remove(ev);
await _context.SaveChangesAsync();
}
return RedirectToAction("Index", "Home");
}
}
}