3

我在 MVC2 中使用 Ajax.BeginForm 进行 ajax 调用,如下所示:

using (Ajax.BeginForm("methoname", "controller", new AjaxOptions
{
   HttpMethod = "Post",
   OnSuccess = "OnSuccessUserSave",
   UpdateTargetId = "PopupBody"

}))

现在,我想在请求发送时显示加载并在请求完成/成功/失败时停止它。

我知道我可以使用

OnBegin = "startLoading",
OnComplete = "stopLoading",
OnFailure = "stopLoading",
OnSuccess = "stopLoading"

但是,我不想这样。

我有很多这样的要求。

所以,我想用 jQuery 制作。

一旦我编写了 jquery 脚本,它就会为每个请求触发。

我喜欢这样:

$(document).ajaxSuccess(function () {
  $('#LoadingImage').modal('hide');
});

$(document).ajaxComplete(function () {
    $('#LoadingImage').modal('hide');
});

$(document).ajaxSend(function () {
    $('#LoadingImage').modal('show');
});

$(document).ajaxError(function () {
    $('#LoadingImage').modal('hide');
});

但是,它仅适用于以下语法:

 $.ajax({
    type: "GET",
    url: "NewUser/Testing",
    dataType: "json",
    // cache: false,
    success: function (response) {
        alert("Request Success");  
    },
    error: function (xhr, status) {
        alert('Unknown error ' + status); 
    }
 });

而且,它不适用于 Ajax.BeginForm。

我应该怎么做它与 jquery 一起使用?

任何帮助将不胜感激。

提前致谢。

4

1 回答 1

0

MVC 框架完美地集成了 jQuery。我建议您也将 jQuery 用于表单。在这种情况下,使用Html.BeginForm而不是Ajax.BeginForm这样的东西:

('form').on('submit',function(){
     var form = $(this);
     $.ajax({
        url: form.attr('action'),
        data: form.serialize(),
        dataType: 'json',
        success : function(data){//success function here},
        error : function(resp, status, xhr){}
  });
});
于 2013-06-20T13:02:46.970 回答