jQuery post和.NET MVC操作 - 将部分结果返回给最终用户

时间:2017-07-05 12:12:14

标签: javascript jquery asp.net asp.net-mvc asp.net-mvc-4

我对以前没有机会遇到的概念有疑问,但我已经考虑了很长时间了,我想看看它是否可以在.NET MVC 5中使用jQuery ...

因此,假设我们在控制器中有一个动作,如下所示:

[ActionName("OurTestAction")]
[HttpPost]
public ActionResult OurTestAction()
{
 Method1();
//returning some sort of "signal" here to client side to notify that the method1 finished processing data.. ?
 Method2();
 Method3();
 return Json("Ok");
}

行动的触发点如下:

$(document).ready(function(){
$.post("/Controller/OurTestAction").done(function(response){
// displaying the result here
});
}):

所以我对这段代码的想法是,我能够告诉最终用户何时调用Method1()并结束它的生命周期。一旦Method1();完成它应该做的事情,我希望能够在浏览器中告诉最终用户这样的事情(通过jQuery):Method1()完成处理数据,转移到method2();

jQuery和.NET是否可以这样做,如果是这样的话?

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

将控制器方法分解为不同的操作。

<强>控制器

[HttpPost]
public ActionResult Method1()
{
  Method1();
  return Json("Ok");
}



[HttpPost]
public ActionResult Method2()
{
  Method2();
  return Json("Ok");
}


[HttpPost]
public ActionResult Method3()
{
  Method3();
  return Json("Ok");
}

<强>的Javascript

$.ajax({
    type: 'POST',
    url: 'controller/method1'
}).done(function(resp) {
   //do stuff to notify users method 1 is complete
    $.ajax({
        type: 'POST', 
        url: 'controller/method2'
    }).done(function(resp2) {
        //do stuff to notify users method 2 is complete
    });
});