Вопрос

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.

Это было полезно?

Решение

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.

Другие советы

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",.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top