Problems doing a proper HTTP Delete with Ajax.ActionLink
What i'm trying to do: Try to dele开发者_开发问答te 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.
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.
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.
精彩评论