Problems doing proper HTTP removal using Ajax.ActionLink

What I'm trying to do: Try deleting the entry using the "correct" HTTP deletion.

Controller Code:

[HttpDelete] public void DeleteRun(int RunId) { repository.RemoveEntry(RunId); } 

Shaver Type:

  @Ajax.ActionLink("Delete","DeleteRun",new {RunId = run.RunId}, new AjaxOptions() { Confirm = "Are you sure you want to delete this entry?", HttpMethod = "DELETE", OnComplete = string.Format("DeleteRunInTable({0})",run.RunId) }) 

Javascript (in a separate file):

  function DeleteRunInTable(RunId) { $("tr[data-runid=" + RunId).remove(); } 

A link to the actionlink method creates:

  <a data-ajax="true" data-ajax-complete="DeleteRunInTable(11)" data-ajax-confirm="Are you sure you want to delete this entry?" data-ajax-method="DELETE" href="/Runs/Delete/11">Delete</a> 

Not sure if part of javascript is working, but not worried about it. Trying to do it step by step :). Now it just works like a traditional tag, and when I click the link, I just execute the href GET request. Of course, I get a 404 error due to the fact that [HTTPDelete] I put on the controller. I am new to web development, so I'm sure there are other ways in javascript or jquery to do the same, but I'm just doing what I know at the moment.

+6
source share
2 answers

This should work the way I did it myself recently, and all I had to do was specify HttpMethod in the AjaxOptions argument.

You also need to make sure that the jquery.unobtrusive-ajax.js script is included on the page.

+9
source

Actually it was a simple solution .... I was missing jquery.unobtrusive-ajax.min.js: P. I leave a message here, so anyone trying to do something similar to what I do will know its possible, just make sure you include jquery and jquery.unobtrusive.

Edit: just to clarify that ActionLink works with jQuery if you use MVC3, otherwise it uses javascript libraries for Microsoft.

+4
source

Source: https://habr.com/ru/post/894234/


All Articles