Async ASP.NET MVC and JavaScript

I need to do something that I am pretty sure that ASP.NET MVC would allow, but I am not sure how to implement.

My overall goal is to find an easy way to update the with status of a server side process.  Is there a way to make JS callbacks from c# code in the ASP.NET MVC framework?

What I need to do is call an c# method, ActionResult, or WebService that can call JavaScript on the page as it processes.  I need a way to return more than just one result.


This method would be called from JS, JQuery .getJSON or something...

public JsonResult SaveFile() {
     //Code to process file

     //Call To JavaScript on page
     JS.UpdateUser("File Recieved, Processing now...");

     //Some more Server side c# Code

     //A second Call to JS
     JS.UpdateUser("Almost Finished... Transferring now...");

     return [JSON Var of some sort]

So the goal of this woudl be to implement a lager process and send the page status updates by makign JS calls somehow.  Another layer on this would be to get user info if possible.


public JsonResult FileWizard() {

    //Some c# Code

   //A call to JavaScript on the page that will require user input
   var userCount = JS.Prompt("How many people will view this file?");

   //At this point the server side process would pause to wait for user prompt info, once received it would continue.

I understand that the second example may be asking one method to do too much, and perhaps a chain of methods is required for this type of code.  This most likely is not the way to do it, but how would I implement simple code to accomplish the same goal using ASP.NET MVC?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The problem with what you are attempting is that there is no direct interaction between the server code and the client code.  As such you cannot call client javascript from the server.  What you can do, however, is run a setTimeOut in the client javascript and use that to check every 5 seconds or so for updates via a webservice.  This will initiate all your callbacks from the client, and if your value has changed update from the client side script.

Another option is to break your server script up into multiple blocks, where the return from the first block is "File Received.  Processing Now...", and the return function then calls the second block, which updates the status "Almost finished.  Updating now...".  That function would then call a third service function to retrieve the actual data.  This is a little more cumbersome to use.  By preference I would use the first, and set the timeout value to 10 or 20, and will get the "Status" value.

Note that in the "Status" you can send back JSON values and use one of those to determine what to do, so that you can if you want pass a request for "How many people will view this" and send that data back to the appropriate service.  Just remember that all javascript needs to be initiated from the client side and you will be in good shape.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
al-w-bAuthor Commented:
Thanks Geoff,

So I came up with the second one as a solution for myself, but the first is so much more elegant, and what I was looking for with the question.  Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.