Post with jQuery not working in Asp.Net MVC?
I'm trying to make a post call to an action method in MVC 2. But it doesn't seem to work. I have set a break point right at the beginning of the action method, but the debugger never hits the breakpoint. What am I doing wrong? Here's the jQuery (for simplicity I have simplified it by removing parameters both from the call and the action method). Please note that the callback alert does get called, which makes it even stranger.
$("#deleteFile").click(function () {
var url = '<%= Url.Action("DeleteFile", "Customers") %>';
$.post(url,
null,
function (data) {
alert("POSTING TO ACTION METHOD");
}
);
});
The action method signature 开发者_运维问答looks like this:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DeleteFile()
{ ... etc }
Also, other jQuery calls have worked against the action methods, with or without parameters, e.g. the .load function.
Ah, never mind, I found the problem myself. The element with the id deleteFile was a link, and I had not used event.preventDefault(); so there was an empty href, which made the page reload instead of posting via jQuery. Once I added that it worked :-)
精彩评论