Part 5 — Add Create, Edit, and Delete Operations (Full CRUD)
In this part, you will implement Create, Edit, and Delete actions for the Student entity using MVC + EF Core.
We already completed Read/List operations in Part 4. Now let’s complete the remaining CRUD steps.
1. Create Operation (Insert New Student)
1.1 Add GET Action (Show Form)
public IActionResult Create()
{
return View();
}1.2 Add POST Action (Save New Student)
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Student student)
{
if (ModelState.IsValid)
{
_context.Students.Add(student);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(student);
}2. Create.cshtml — Add the View
Create file:
Views/Students/Create.cshtml@model Student
<h2>Create Student</h2>
<form asp-action="Create">
<div class="form-group">
<label asp-for="Name"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Age"></label>
<input asp-for="Age" class="form-control" />
<span asp-validation-for="Age" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary">Save</button>
<a asp-action="Index" class="btn btn-secondary">Back</a>
</form>3. Edit Operation (Update Student Details)
3.1 Add GET Action (Load Data to Edit)
public async Task<IActionResult> Edit(int id)
{
var student = await _context.Students.FindAsync(id);
if (student == null)
return NotFound();
return View(student);
}3.2 Add POST Action (Perform Update)
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, Student student)
{
if (id != student.Id)
return NotFound();
if (ModelState.IsValid)
{
_context.Update(student);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(student);
}4. Edit.cshtml — Add the View
Create file:
Views/Students/Edit.cshtml@model Student
<h2>Edit Student</h2>
<form asp-action="Edit">
<input type="hidden" asp-for="Id" />
<div class="form-group">
<label asp-for="Name"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Age"></label>
<input asp-for="Age" class="form-control" />
<span asp-validation-for="Age" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary">Update</button>
<a asp-action="Index" class="btn btn-secondary">Back</a>
</form>5. Delete Operation (Remove Student)
5.1 Add GET Action (Show Confirmation Page)
public async Task<IActionResult> Delete(int id)
{
var student = await _context.Students.FindAsync(id);
if (student == null)
return NotFound();
return View(student);
}5.2 Add POST Action (Delete the Record)
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var student = await _context.Students.FindAsync(id);
if (student != null)
{
_context.Students.Remove(student);
await _context.SaveChangesAsync();
}
return RedirectToAction(nameof(Index));
}6. Delete.cshtml — Add the View
Create file:
Views/Students/Delete.cshtml@model Student
<h2>Delete Student</h2>
<h4>Are you sure you want to delete this student?</h4>
<div>
<strong>Name:</strong> @Model.Name <br />
<strong>Age:</strong> @Model.Age
</div>
<form asp-action="Delete">
<input type="hidden" asp-for="Id" />
<button type="submit" class="btn btn-danger">Delete</button>
<a asp-action="Index" class="btn btn-secondary">Cancel</a>
</form>7. Add Edit/Delete Buttons in Index.cshtml
Update your table:
<td>
<a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-sm btn-warning">Edit</a>
<a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-sm btn-danger">Delete</a>
</td>Conclusion of Part 5
You have now implemented:
✔ Create
✔ Read
✔ Update
✔ Delete
Your ASP.NET Core MVC beginner CRUD module is now complete.

Join the conversation! Your thoughts help the community grow.