Form submitted twice after updating from ASP MVC 3

2020-02-13 07:26发布

After upgrading my ASP MVC from 3 Preview to 3 Beta I see strange behaviour in my Ajax forms.

@using(Ajax.BeginForm("New", new AjaxOptions() {OnSuccess = "onAjaxSuccess", OnFailure = "onAjaxFailure", OnBegin = "onAjaxBegin", HttpMethod = "Post"})) {}

<form action="/Order/New" data-ajax="true" data-ajax-begin="onAjaxBegin" data-ajax-failure="onAjaxFailure" data-ajax-method="Post" data-ajax-success="onAjaxSuccess" method="post"></form>

I have placed an alert inside my function onAjaxBegin and it is beeing fired twice, each time i click on my submit button.

Anyone else seen this behaviour? I have not changed anything in the code after upgrading, and it worked perfectly before the upgrade.

13条回答
男人必须洒脱
2楼-- · 2020-02-13 07:49

one small tip all the time before you using .live(), do .die() :)
this will kill all the java scripts attached to this event and will create a new one.

example:

$(function() {
    $('#MyInfoForm').die();
    $("#MyInfoForm").live('submit', function(e) {
        //some code
        e.preventDefault();
    });
});
查看更多
来,给爷笑一个
3楼-- · 2020-02-13 07:50

I had the same problem. I had a login partial view in master page that included jquery.unobtrusive-ajax.min.js. I also had this file included in my view. So I removed one and the problem is solved now.

查看更多
甜甜的少女心
4楼-- · 2020-02-13 08:00

You can use bind instead of live in jquery.unobtrusive-ajax.min.js~~~ This is very important.Because live event will call the previous events every time but bind only calls the current event.

查看更多
Melony?
5楼-- · 2020-02-13 08:00

Moving jquery.unobtrusive-ajax.js outside partial-view solved the issue in my case.

查看更多
Deceive 欺骗
6楼-- · 2020-02-13 08:01

I had the same problem, and i found the solution: I included the "jquery.unobtrusive-ajax.min.js"-Script twice :-)

查看更多
Animai°情兽
7楼-- · 2020-02-13 08:02

this is old but I found something really stupid that is easy to overlook.

Our form also had the following attached to it:

$(document).ready(function () {
    $('#submit-button').on('click', function (e) {
        ....
    }
});

Obviously the .NET code handles this itself.

查看更多
登录 后发表回答