ASP NET Core 3 1 Build a Complete MVC ToDo List Application Part 11
This is part 11 of the current playlist of ASP.NET Core 3.1 lecture videos. In this lecture we will: Walkthrough building the same todo list application with ASP.NET Core MVC that we built using Razor Pages (Part 10). Along the way, we shall note the difference in the approaches in building the same application in two different ways. We shall make use of the same ToDo list model and the ToDoService class. However, the additional steps of creating a suitable view model to render a view with the data we want are explained. The controller and the Category action model have been explained and the changes to the Service class (to make it more scalable) have been shown in code. The conventional and attribute routing to show the todo view have been discussed. The analogy of MVC Controller and action method with the Razor PageModel class and the Page Handler method have been brought to light.
#ASPNETCORE31 #RazorPage #MVCDesignPattern #todolistappmvc #MVCController #Routing #ActionMethod
Code: ToDoController
using ASPNETCoreWebApplication.Services;
using ASPNETCoreWebApplication.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace ASPNETCoreWebApplication.Controllers
{
public class ToDoController : Controller
{
private readonly ToDoService _service;
public ToDoController(ToDoService service)
{
_service = service;
}
[Route("/ToDo/Category/{category}")] //For attribute routing
public ActionResult Category([FromRoute]string category)
{
CategoryViewModel viewModel = new CategoryViewModel();
viewModel.ListModels = _service.GetItemsForCategory(category);
return View(viewModel);
}
}
}
View (ToDo.cshtml)
@using ASPNETCoreWebApplication.ViewModels
@model CategoryViewModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Category</title>
</head>
<body>
<table class="table">
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
@{ foreach (var item in Model.ListModels)
{
<tr>
<td>
Item is: @item.Name
</td>
</tr>
}
}
</tbody>
</table>
</body>
</html>
ViewModel:
using ASPNETCoreWebApplication.Models;
using System.Collections.Generic;
namespace ASPNETCoreWebApplication.ViewModels
{
public class CategoryViewModel
{
public IEnumerable<ToDoListModel> ListModels{ get; set; }
public string currentCategory { get; set; }
}
}
ToDoService.cs (Modified)
using ASPNETCoreWebApplication.Models;
using System.Collections.Generic;
using System.Runtime.InteropServices.ComTypes;
namespace ASPNETCoreWebApplication.Services
{
public class ToDoService
{
List<ToDoListModel> toDoLists = new List<ToDoListModel>();
public List<ToDoListModel> GetItemsForCategory(string category)
{
switch(category)
{
case "HomeItems":
toDoLists.Add(new ToDoListModel { id = 1, Name = "Homework" });
toDoLists.Add(new ToDoListModel { id = 2, Name = "Do dishes" });
toDoLists.Add(new ToDoListModel { id = 3, Name = "Visit Market" });
break;
case "WorkItems":
toDoLists.Add(new ToDoListModel { id = 1, Name = "Planning" });
toDoLists.Add(new ToDoListModel { id = 2, Name = "Coding" });
toDoLists.Add(new ToDoListModel { id = 3, Name = "Testing" });
break;
default:
toDoLists.Add(new ToDoListModel { id = 1, Name = "Playing" });
toDoLists.Add(new ToDoListModel { id = 2, Name = "Singing" });
toDoLists.Add(new ToDoListModel { id = 3, Name = "Camping" });
break;
}
return toDoLists;
}
}
}
Startup.cs (Changes in bold)(for conventional routing)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//app.UseWelcomePage("/");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler
(
"/Error"
);
}
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints
(
endpoints => {
endpoints.MapControllerRoute(name: "todo",
pattern: "{controller=ToDo}/{action=Category}/{category}");
endpoints.MapControllerRoute(name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
Comments
Post a Comment