c# - MVC JsonResult updating progress -
this question has answer here:
mvc 4 webapp, jquery
i want show progress of lenghty procedure.
the following gets called json takes several seconds...
basically, is:
[httppost] public jsonresult update(var x) { return json(new { result = "working", message = "waiting on server" }); var y = contactanotherserver(x); return json(new { result = "working", message = "crunching data" }); var finished = dosomethingwithdata(y); return json(new { result = "ok", message = "done" }); }
obviously terminate on first return-statement.
how done ?
thank you!
to "roll own" this, need 3 separate jsonresult methods , complex javascript function. ie:
controller
public jsonresult step1() { return json(new { result = "working", message = "waiting on server" }); } public jsonresult step2() { return json(new { result = "working", message = "crunching data" }); } public jsonresult step3() { return json(new { result = "ok", message = "done" }); }
js
// urls var urls = ["/step1","/step2","/step3"]; // counter var counter = 0; // recursive method var fetchandupdaterecursive(url){ // data $.ajax(url, { async: false, success: function(r){ // use r update visually // then... // change url counter++; url = urls[counter]; // go again fetchandupdaterecursive(url); }; }); }; // go var urltostartwith = urls[counter]; fetchandupdaterecursive(urltostartwith);
Comments
Post a Comment