Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems doing a proper HTTP Delete with Ajax.ActionLink

What i'm trying to do: Try to delete a record using a "proper" HTTP Delete.

Controller Code:

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

    }

Razor View:

             @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 separate included file):

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

Link the actionlink method is creating:

 <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 the javascript part works yet but not to worried about it. Trying to take it one step at a time :). Now its just working like a traditional tag and when i click the link its just doing a GET request of the href. Of course i get a 404 error because of the [HTTPDelete] i put on my controller. I'm pretty new to web development so i'm sure there are other ways in either javascript or jquery to do the same thing but i'm just doing what i know at this point.

like image 488
coding4fun Avatar asked Aug 03 '11 21:08

coding4fun


1 Answers

This should work as I have done it myself recently and all I had to do was specify the HttpMethod in the AjaxOptions argument.

You also need to ensure you have the jquery.unobtrusive-ajax.js script included on the page.

like image 116
lomaxx Avatar answered Sep 17 '22 20:09

lomaxx