如何在ASP.NET MVC中的ajax调用上执行多个服务器端方法?

时间:2015-04-21 11:32:58

标签: javascript jquery ajax asp.net-mvc

我有一个方法,由jquery调用,其结果显示在popUp上。有时候需要时间来完成并且处理时出现空白弹出窗口。当点击关闭弹出窗口消失但方法在服务器端继续运行时。我想通过ajax调用停止执行另一个方法,我知道服务器端除了请求之外都没有完成早先的要求。 我该怎么办??我不能使用异步调用。

$.ajax({
  url: '@Url.Action("StopMyMethod")',
  type: 'GET',
  cache: false,
  data: { boolValue:true },
  success: function (data) { 
    alert('success'); 
  },
  error: function (data) {  
    alert('Error'); 
  }
});

和服务器端代码是

Boolean StopOvsPersonalDataDetail = false;
[HttpGet]
public ActionResult MyMethod(int id){
  foreach(var a from list)
  {
    if (StopOvsPersonalDataDetail)
      break;
    }
  return View(list);
}

[HttpGet]
public ActionResult  StopMyMethod(Boolean boolValue){
  StopOvsPersonalDataDetail=true;
  return view;           
  }

1 个答案:

答案 0 :(得分:0)

你可以试试这个。可能会有所帮助......

         $.ajax({
            type: "Get",
            url:'@Url.Action("StopMyMethod")',
            data: { boolValue:true }, //Pass as required
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            error: function ('data') {
            alert('Error') },
            success: function (data) 
            {
            if (data == true) {
              //required code here
            }
            else{
               //show error
                 alert('alert');
                }
            }
        });

 [httpGet]
 public  ActionResult MyMethod(int id)
 {
  try
   {  
    foreach(var a from list)
    {
      if (StopOvsPersonalDataDetail)
       break;
    }
        return View(list);
  }
    else
    {
        return false;
    }
}
catch ( Exception ex )
{
    return false;
}