I have started reading the 2nd edition of the Pro ASP.NET Core MVC book and after reading the first couple of chapters, I have decided to make a video of the application in the second chapter. The code is about 80% the same, with some minor changes from my side. This is what the application does:
As far as this is a simple application, it does not do a lot, indeed. However, it took me about 1 hour to make it “running”. This is how the HomeController.cs looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
using System; using System.Linq; using Microsoft.AspNetCore.Mvc; using PartyInvites.Models; namespace PartyInvites.Controllers { public class HomeController : Controller { public ViewResult Index() { int hour = DateTime.Now.Hour; ViewBag.Greeting = hour < 12 ? "Good morning" : "Good afternoon"; return View("MyView"); } [HttpGet] public ViewResult RsvpForm() { return View(); } [HttpPost] public ViewResult RsvpForm(GuestResponse guestResponse) { if (ModelState.IsValid) { Repository.AddResponse(guestResponse); return View("Thanks", guestResponse); } else { return View(); } } public ViewResult ListResponses() { return View(Repository.Responses.Where(r => r.WillAttend == true)); } } } |
This is the Repository.cs :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System.Collections.Generic; namespace PartyInvites.Models { public class Repository { private static List<GuestResponse> responses = new List(); public static IEnumerable Responses { get { return responses; } } public static void AddResponse(GuestResponse response) { responses.Add(response); } } } |
The video is available here:
The GitHub for the whole code is here – Github.com/Vitosh/ASP/tree/master/PartyInvites
Enjoy it! 🙂