Simon M.
Simon M.

Reputation: 2482

MVC action in ASP .NET throws 404 error

I have to add a delete method to an existing application designed with ASP .NET, the thing is that I have 0 experience with. Here is what I've done :

CONTROLLER

[HttpPost, Authorize(Roles = "Admin")]
public ActionResult TypeLotRemove(Guid id)
{
    TypeLot typeLot = _repository.GetTypeLot(id);

    //On supprime le type de lot à la base.
    _repository.RemoveTypeLot(typeLot);
    return RedirectToAction("TypeLot");
}

REPOSITORY

public TypeLot RemoveTypeLot(TypeLot typeLot)
{
    _entities.RemoveToTypeLot(typeLot);
    _entities.SaveChanges();
    return typeLot;
}

Designer.cs

[global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
public void RemoveToTypeLot(TypeLot typeLot)
{
    base.DeleteObject(typeLot);
}

VIEW

<%= Html.ActionLink("Supprimer","TypeLotRemove", new { id = item.ID} )%>

When I click on the link, I'm redirected to TypeLotRemove/420c039f-516e-40c5-916f-6da222eb50ae so I have a 404 error (normal cause I don't have TypeLotRemove file) and nothing is done. What did I missed ?

Upvotes: 0

Views: 1337

Answers (2)

anmarti
anmarti

Reputation: 5143

Change HttPost for HttpGetin method TypeLotRemove

Upvotes: 1

Jamie Rees
Jamie Rees

Reputation: 8183

The issue is that you are just generating a link to the ActionMethod, when you have decorated the ActionMethod with the [HttpPost] verb. This means that this ActionMethod is only accessible via a POST request.

Using [HttpPost] makes sense in this case so just do this in your View:

@using(Html.BeginForm("TypeLotRemove", "Controller", FormMethod.Post)
{
   // Your form and submit
}

Also pointed out by mstaessen in the comments you are attempting to access the URL /TypeLotRemove/1 again this is a GET request but the parameter you are passing (in this case 1) is not a GUID that you require in your ActionMethod

Upvotes: 2

Related Questions