Great! Here's a little longer ASP.
NET Core MVC project that includes:
● Model
● Controller
● Views (Create, Read, Edit, Delete)
● Basic validation
● Tag Helpers
● In-memory list for data (no database for simplicity)
📘 Project Name: Task Tracker App
✅ Features:
● Add new task
● View all tasks
● Edit a task
● Delete a task
● Use Tag Helpers and Model Validation
🧱 Folder Structure
pgsql
CopyEdit
TaskTrackerApp/
│
├── Controllers/
│ └── TaskController.cs
│
├── Models/
│ └── TaskItem.cs
│
├── Views/
│ └── Task/
│ ├── Index.cshtml
│ ├── Create.cshtml
│ ├── Edit.cshtml
│ └── Delete.cshtml
│
├── wwwroot/
├── Program.cs
├── Startup.cs (if applicable)
└── TaskTrackerApp.csproj
🔧 Step-by-Step Instructions
Step 1: Create Project
● Open Visual Studio
● Select ASP.NET Core Web App (MVC)
● Name: TaskTrackerApp
● Click Create
Step 2: Create the Model
📄 Models/TaskItem.cs
csharp
CopyEdit
using System.ComponentModel.DataAnnotations;
namespace TaskTrackerApp.Models
{
public class TaskItem
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Display(Name = "Is Completed")]
public bool IsCompleted { get; set; }
}
}
Step 3: Create the Controller
📄 Controllers/TaskController.cs
csharp
CopyEdit
using Microsoft.AspNetCore.Mvc;
using TaskTrackerApp.Models;
using System.Collections.Generic;
using System.Linq;
namespace TaskTrackerApp.Controllers
{
public class TaskController : Controller
{
static List<TaskItem> tasks = new List<TaskItem>();
public IActionResult Index()
{
return View(tasks);
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(TaskItem task)
{
if (ModelState.IsValid)
{
task.Id = tasks.Count + 1;
tasks.Add(task);
return RedirectToAction("Index");
}
return View(task);
}
public IActionResult Edit(int id)
{
var task = tasks.FirstOrDefault(t => t.Id == id);
return View(task);
}
[HttpPost]
public IActionResult Edit(TaskItem updatedTask)
{
var task = tasks.FirstOrDefault(t => t.Id ==
updatedTask.Id);
if (task != null && ModelState.IsValid)
{
task.Title = updatedTask.Title;
task.IsCompleted = updatedTask.IsCompleted;
return RedirectToAction("Index");
}
return View(updatedTask);
}
public IActionResult Delete(int id)
{
var task = tasks.FirstOrDefault(t => t.Id == id);
return View(task);
}
[HttpPost, ActionName("Delete")]
public IActionResult DeleteConfirmed(int id)
{
var task = tasks.FirstOrDefault(t => t.Id == id);
if (task != null)
{
tasks.Remove(task);
}
return RedirectToAction("Index");
}
}
}
Step 4: Create Views
📁 Views/Task/Index.cshtml
html
CopyEdit
@model List<TaskTrackerApp.Models.TaskItem>
<h2>Task List</h2>
<a asp-action="Create">Add Task</a>
<table border="1">
<tr><th>ID</th><th>Title</th><th>Completed</th><th>Actions</th></tr>
@foreach (var task in Model)
{
<tr>
<td>@task.Id</td>
<td>@task.Title</td>
<td>@task.IsCompleted</td>
<td>
<a asp-action="Edit"
asp-route-id="@task.Id">Edit</a> |
<a asp-action="Delete"
asp-route-id="@task.Id">Delete</a>
</td>
</tr>
}
</table>
📁 Views/Task/Create.cshtml
html
CopyEdit
@model TaskTrackerApp.Models.TaskItem
<h2>Add New Task</h2>
<form asp-action="Create" method="post">
<div>
<label asp-for="Title"></label>
<input asp-for="Title" />
<span asp-validation-for="Title"></span>
</div>
<div>
<label asp-for="IsCompleted"></label>
<input asp-for="IsCompleted" type="checkbox" />
</div>
<button type="submit">Save</button>
</form>
📁 Views/Task/Edit.cshtml
html
CopyEdit
@model TaskTrackerApp.Models.TaskItem
<h2>Edit Task</h2>
<form asp-action="Edit" method="post">
<input type="hidden" asp-for="Id" />
<div>
<label asp-for="Title"></label>
<input asp-for="Title" />
<span asp-validation-for="Title"></span>
</div>
<div>
<label asp-for="IsCompleted"></label>
<input asp-for="IsCompleted" type="checkbox" />
</div>
<button type="submit">Update</button>
</form>
📁 Views/Task/Delete.cshtml
html
CopyEdit
@model TaskTrackerApp.Models.TaskItem
<h2>Delete Task</h2>
<form asp-action="Delete">
<input type="hidden" asp-for="Id" />
<p>Are you sure you want to delete <b>@Model.Title</b>?</p>
<button type="submit">Yes, Delete</button>
</form>
▶️ Run the App:
● Navigate to /Task
● Add, edit, and delete tasks
● Data will be lost on restart (in-memory only)