0

I try to implement DELETE method for my mvc-application.

There is my link placed in View:

@Ajax.ActionLink("Delete", "Delete", "Users", new { userId = user.Id }, new AjaxOptions { HttpMethod = "DELETE" })

There is my controller method:

[HttpDelete]
public ActionResult Delete(string userId)
{
    //...
    return View("Index");
}

When I click on Delete-link, I get a 404 error.

In Fiddler I see, that my request perfomed by GET method! If I execute DELETE request with the same Headers from Fiddler, I get the expected result - my request is coming right into the Delete method.

How can I use @Ajax.ActionLink correctly?

P.S.: I want to use only built-in methods.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Dem0n13
  • 766
  • 1
  • 13
  • 37

2 Answers2

2

Are you sure all the Unobtrusive libraries are loaded? An @Ajax.ActionLink generates a standard anchor tag. If the JavaScript libraries aren't loaded to handle the click event, you'll get the GET request you see in Fiddler.

Check to see if the jquery.unobtrusive-ajax.js script is included in a bundle that is referenced from your layout page or that you're explicitly loading it on specific pages in a scripts region.

Erik Noren
  • 4,279
  • 1
  • 23
  • 29
  • The next libraries are loaded: Modernizr v2.7.2, jQuery v2.1.0, jQuery Validation 1.11.1, Bootstrap v3.1.1, jquery validate unobtrusive – Dem0n13 Mar 15 '14 at 01:00
  • 1
    The fact your link click is executing a GET request tells me something isn't handling the click and preventing the default behavior. That suggests a missing library, likely the unobtrusive libs since this is the same thing that happened to me when I failed to add them to my specific views. – Erik Noren Mar 15 '14 at 02:19
  • You can try changing the HttpMethod to POST and seeing if that works. If you are still receiving a GET request, I'd point you back to a likely problem with the libs. If it works then we have a better place to start bisecting this problem. – Erik Noren Mar 15 '14 at 02:20
  • Doing a little searching just to make sure DELETE is a supported type, this answer suggests others have it working once the right libs are in place. http://stackoverflow.com/questions/6933702/problems-doing-a-proper-http-delete-with-ajax-actionlink – Erik Noren Mar 15 '14 at 02:23
  • I found that the `jquery.unobtrusive-ajax.js` is not included in any script bundles. Now everything works, thank you! – Dem0n13 Mar 15 '14 at 12:02
0

Try this:

@Ajax.ActionLink("Delete", "Delete", "Users", 
  new { userId = user.Id }, 
  new AjaxOptions { HttpMethod = "POST" })

I am not sure why you used 'Delete' for the HTTPMethod. Post will send the Id for data you want removed to the server and call the 'Delete' ActionResult specified here @Ajax.ActionLink("Delete", "Delete", "Users",.

stink
  • 865
  • 8
  • 19