Problems doing a proper HTTP Delete with Ajax.ActionLink

喜你入骨 提交于 2019-12-07 03:23:53

问题


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.


回答1:


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.




回答2:


It was actually a simple solution....i was missing the jquery.unobtrusive-ajax.min.js :P. I'm leaving the post here so anyone trying to do something similar to what i'm doing will know its possible just make sure you include jquery & jquery.unobtrusive.

Edit: Just to clarify ActionLink works with JQuery if your using MVC3 otherwise it uses the microsoft javascript libraries.



来源:https://stackoverflow.com/questions/6933702/problems-doing-a-proper-http-delete-with-ajax-actionlink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!