I have an ASP.NET Core application with a RobotController that includes a Delete method. I also have Details and Delete views. In the Details view, there is a "Delete" button that should navigate to the Delete action in the RobotController. However, when I click the "Delete" button, I get a "404 Not Found" error
Here is my Delete and DeleteConfirmed methods of RobotController
[HttpGet]
public async Task<IActionResult> Delete(string id)
{
if (id == null)
{
return NotFound();
}
var robot = await _robotRepo.GetRobotByIdAsync(id);
if (robot == null)
{
return NotFound();
}
var robotDetails = new RobotDetailsViewModel
{
Id = robot.Id,
Name = robot.Name
};
return View("Delete", robotDetails);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(string id)
{
await _robotRepo.DeleteRobotByIdAsync(id);
TempData["SuccessMessage"] = "Robot deleted successfully";
return RedirectToAction("Index");
}
Here is my Details view:
@model AnyTaskRobotStore.ViewModels.RobotDetailsViewModel
<div class="container mt-5">
<div class="row">
<div class="col-md-6">
<img src="@Model.Image" class="img-fluid" alt="@Model.Name" />
</div>
<div class="col-md-6">
@* Lots of markup *@
<div class="mt-3">
@* Buttons *@
<a asp-action="Delete" asp-route-id="@Model.Id" class="btn btn-danger mx-2"><i class="fa-solid fa-trash"></i> Delete</a>
</div>
</div>
</div>
</div>
And here is my Delete view:
@model AnyTaskRobotStore.ViewModels.RobotDetailsViewModel
<div class="container mt-5">
<h2>Delete Robot</h2>
<p>Are you sure you want to delete this robot: @Model.Name?</p>
<form asp-action="DeleteConfirmed" method="post">
<input type="hidden" asp-for="Id" />
<div class="form-group">
<input type="submit" value="Delete" class="btn btn-danger" id="deleteButton" />
<a asp-action="Details" asp-route-id="@Model.Id" class="btn btn-secondary ms-2">Back to Details</a>
</div>
</form>
</div>
I have verified that the Delete and Details views exist in the Views/Robot folder. Despite this, I still encounter the "404 Not Found" error. What could be causing this issue, and how can I resolve it?