0

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> &nbsp; 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?

2
  • It seems I've figured out the issue. The id is coming in as null Commented Dec 6, 2024 at 16:47
  • use <input type="hidden" name="id" value="@Model.Id" /> Commented Dec 6, 2024 at 20:24

1 Answer 1

-1

As per your code, You have mentioned if id is null then return NotFound(); or if robot == null then return NotFound();

So make sure, while debugging the code, id i having value and robot also having the value, otherwise you will end up with 404.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.