开发者

Post with jQuery not working in Asp.Net MVC?

开发者 https://www.devze.com 2023-01-14 09:33 出处:网络
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 breakp

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 :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号