tags:

views:

1185

answers:

5

A page executes a number of tasks and takes a long time to process. We want to give the user feedback as each task is completed.

In ASP.NET webforms we used Response.Flush()

What way would you a approach this in ASP.NET MVC?

A: 

You can make it in client side. In each step, you set some session variable with the current step. Then, You make another action in your controller say called: "GetProgress" and assign a view and URI for it.

In the action, you will check this session and return the current progress of your task. In the client side, make a timer (i.e setTimeOut) and you invoke the URI of the later controller action every specific amount of time - 1 second or so. That is it.

mnour
+1  A: 

Me personally I would consider two optoins:

  • redirect to wait page(s), then fire actions
  • Do it ajax style
borisCallens
+2  A: 

I would suggest to use AJAX for displaying progress. See links for ideas:

Panos
+4  A: 

You can still use Response.Write() and Response.Flush() for whatever status you want to send down the wire. Or if you have your progress thingy in a user-control, you could do something like:

this.PartialView("Progress").ExecuteResult(this.ControllerContext);
this.Response.Flush();

from your controller while doing your lengthy operation in the controller's action method.

It's up to you to choose this or the client-side approach as mentioned in the comments here, just wanted to point out that server-side is still possible.

liggett78
I have a partial view with the text "please wait..."., and use this approach. But the "please wait..." text remains on the page after everything is loaded. An easy way to remove it when the final View is returned from the Action method?
Frode Lillerud
@Frode Lillerud: A piece of Javascript that removes "please wait" from the browser DOM or hides it.
liggett78
+2  A: 

There are two basic ways:

  1. Poll a server page that returns the status, then once the operation is done, redirects to a results page. MVC is nothing to do with this way, you'd need to use a server variable to store objects/status - this is a way that's more relevant to a standard Asp.NET application as you're (presumably) using session variables etc. anyway.

  2. AJAX call from the client to a webservice on the server. Asp.NET MVC is going to be rolling the jQuery framework in, so use that for the client call and event handling for the response. This would be more in the spirit of MVC which doesn't/shouldn't use session state etc.

marcus.greasly