NET 6 - MVC - Controller

Last modified: March 25, 2022

We are looking at another part of MVC, which is Controller. It acts as an interface between the Model and View Component, process business logic, incoming request, data manipulation and finally interact with View to render the HTML.

Let's begin by creating an EventController class. In each UI project, right-click the Controllers folder and then click Add, then Controller. Select "MVC Controller with read/write actions" and name the controller EventController.cs.

private readonly EventService _eventService; public EventController(EventService eventService) { _eventService = eventService; }

We can put HTTP verbs (e.g. GET, POST). If nothing is mentioned for a method, then it is GET, if it is [HttpPost] then it is POST Request. On each form, ASP.NET Core 6 generates a token and pass to the POST method and the POST method validate it.

EventController

public class EventController : Controller { private readonly EventService _eventService; public EventController(EventService eventService) { _eventService = eventService; } // GET: EventController public async Task<ActionResult> Index() { var events = await _eventService.GetAllAsync(); return View(events); } // GET: EventController/Create public ActionResult Create() { return View(); } // POST: EventController/Create [HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> CreateAsync([Bind("Name,Location,Date")] EventViewModel eventViewModel) { try { var add = await _eventService.Add(eventViewModel); return RedirectToAction(nameof(Index)); } catch { return View(); } } // GET: EventController/Edit/5 public async Task<ActionResult> Edit(string id) { var _event = await _eventService.GetById(id); return View(_event); } // POST: EventController/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> Edit(string id, [Bind("Name,Location,Date")] EventViewModel eventViewModel) { try { bool upate = await _eventService.Update(eventViewModel); return RedirectToAction(nameof(Index)); } catch { return View(); } } // GET: EventController/Delete/5 public async Task<ActionResult> Delete(string id) { var _event = await _eventService.GetById(id); return View(_event); } // POST: EventController/Delete/5 [HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> Delete(string id, IFormCollection collection) { try { bool delete = await _eventService.Delete(id); return RedirectToAction(nameof(Index)); } catch { return View(); } } }

EventBookingController

public class EventBookingController : Controller { private readonly EventService _eventService; private readonly EventBookingService _eventBookingService; public EventBookingController(EventService eventService, EventBookingService eventBookingService) { _eventService = eventService; _eventBookingService = eventBookingService; } // GET: EventBookingController public async Task<ActionResult> Index() { var bookings = await _eventBookingService.GetAllAsync(); return View(bookings); } // GET: EventBookingController/Create public async Task<ActionResult> Create() { var eventsViewModel = await _eventService.GetAllAsync(); var eventBookingViewModel = new EventBookingViewModel(); eventBookingViewModel.Events = eventsViewModel; return View(eventBookingViewModel); } // POST: EventBookingController/Create [HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> Create([Bind("EventId,Email")] EventBookingViewModel eventBookingViewModel) { try { var add = await _eventBookingService.AddEventBooking(eventBookingViewModel); return RedirectToAction(nameof(Index)); } catch { return View(); } } }