Creating a web application with ASP.NET could look as a hard task. Still, with the pre-built MVC models from Visual Studio, the task is a bit easier. This is how the application looks like, with 2 search buttons and fully implemented CRUD (Create, Read, Update, Delete) functionality:
The video includes the following steps:
- Getting started with a HelloWorldController.cs
- Add a controller
- Add a view
- Add a model
- Work with SQL Server LocalDB
- Controller methods and views
- Add 2 search fields (one by genre and one by filter word)
- Add a new field (rating 5/7)
- Adding data validation to the model (video at the bottom of the article)
The application is built from scratch (or from New>Project) in the video here:
The most important parts of the application are probably the MoviesController.cs which is about 90% generated by Visual Studio:
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using MvcMovie.Models; namespace MvcMovie.Controllers { public class MoviesController : Controller { private readonly MvcMovieContext _context; public MoviesController(MvcMovieContext context) { _context = context; } // GET: Movies public async Task<IActionResult> Index(string movieGenre, string searchString) { IQueryable<string> genreQuery = from m in _context.Movie orderby m.Genre select m.Genre; var movies = from m in _context.Movie select m; if (!String.IsNullOrEmpty(searchString)) { movies = movies.Where(s => s.Title.Contains(searchString)); } if (!String.IsNullOrEmpty(movieGenre)) { movies = movies.Where(s => s.Genre.Contains(movieGenre)); } var movieGenreVM = new MovieGenreViewModel { Genres = new SelectList(await genreQuery.Distinct().ToListAsync()), Movies = await movies.ToListAsync() }; return View(movieGenreVM); } // GET: Movies/Details/5 public async Task<IActionResult> Details(int? id) { if (id == null) { return NotFound(); } var movie = await _context.Movie .FirstOrDefaultAsync(m => m.Id == id); if (movie == null) { return NotFound(); } return View(movie); } // GET: Movies/Create public IActionResult Create() { return View(); } // POST: Movies/Create // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Create([Bind("Id,Title,ReleaseDate,Genre,Price,Rating")] Movie movie) { if (ModelState.IsValid) { _context.Add(movie); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } return View(movie); } // GET: Movies/Edit/5 public async Task<IActionResult> Edit(int? id) { if (id == null) { return NotFound(); } var movie = await _context.Movie.FindAsync(id); if (movie == null) { return NotFound(); } return View(movie); } // POST: Movies/Edit/5 // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Edit(int id, [Bind("Id,Title,ReleaseDate,Genre,Price,Rating")] Movie movie) { if (id != movie.Id) { return NotFound(); } if (ModelState.IsValid) { try { _context.Update(movie); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!MovieExists(movie.Id)) { return NotFound(); } else { throw; } } return RedirectToAction(nameof(Index)); } return View(movie); } // GET: Movies/Delete/5 public async Task<IActionResult> Delete(int? id) { if (id == null) { return NotFound(); } var movie = await _context.Movie .FirstOrDefaultAsync(m => m.Id == id); if (movie == null) { return NotFound(); } return View(movie); } // POST: Movies/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public async Task<IActionResult> DeleteConfirmed(int id) { var movie = await _context.Movie.FindAsync(id); _context.Movie.Remove(movie); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } private bool MovieExists(int id) { return _context.Movie.Any(e => e.Id == id); } } } |
And the razor view at Views>Movies>Index.cshtm with the two search fields:
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
@model MvcMovie.Models.MovieGenreViewModel @{ ViewData["Title"] = "Index"; } <h2>Index</h2> <p> <a asp-action="Create">Create New</a> </p> <form asp-controller="Movies" asp-action="Index" > <p> Title:<input type="text" name="SearchString" /> <input type ="submit" value="Filter" /> <select asp-for="MovieGenre" asp-items="Model.Genres"> <option value= "">All</option> </select> </p> </form> <table class="table"> <thead> <tr> <th> @Html.DisplayNameFor(model => model.Movies[0].Title) </th> <th> @Html.DisplayNameFor(model => model.Movies[0].ReleaseDate) </th> <th> @Html.DisplayNameFor(model => model.Movies[0].Genre) </th> <th> @Html.DisplayNameFor(model => model.Movies[0].Price) </th> <th> @Html.DisplayNameFor(model => model.Movies[0].Rating) </th> <th></th> </tr> </thead> <tbody> @foreach (var item in Model.Movies) { <tr> <td> @Html.DisplayFor(modelItem => item.Title) </td> <td> @Html.DisplayFor(modelItem => item.ReleaseDate) </td> <td> @Html.DisplayFor(modelItem => item.Genre) </td> <td> @Html.DisplayFor(modelItem => item.Price) </td> <td> @Html.DisplayFor(modelItem => item.Rating) </td> <td> <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> | <a asp-action="Details" asp-route-id="@item.Id">Details</a> | <a asp-action="Delete" asp-route-id="@item.Id">Delete</a> </td> </tr> } </tbody> </table> |
Adding data validation to the project:
The rest is in GitHub here:
https://github.com/Vitosh/ASP/tree/master/MvcMovie
https://github.com/Vitosh/ASP/tree/master/MvcMovieWithDataValidation
Enjoy!