• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 629
  • Last Modified:

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.

Example

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.

Example:

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?
0
al-w-b
Asked:
al-w-b
1 Solution
 
GeoffSuttonCommented:
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.

HTH,
Geoff
0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now