tags:

views:

360

answers:

1

I'm using ASP.Net MVC to create a web site which needs to do some processing (5 - 10 seconds) before it can return a view to the user. Rather than leaving the user staring at the glacial progress bar I'd like to show some sort of "Please Wait/We'll be right back" animated gif to keep them interested.

Does anyone know a good approach to achieving this?

(I found this answer but its not quite what I need, this uses jQuery to fetch data once the view has been returned. I'd like to display the "Please Wait" while they're waiting for the view to appear)

Thanks

+4  A: 

I think the solution you referenced will work for you. You just need to have your initial controller action return right away with the "please wait message", then have the AJAX call do the actual retrieval of the contents based on your processing. If the request really takes 5-10 seconds you may also need to adjust the timeout value on the AJAX request so that it is able to complete. I don't know what the default timeout is but is may be less than what you need.

EDIT Example:

View code:

<script type="text/javascript">
     $(document).ready( function() {
         $.ajax({
            type: "POST",
            url: '<$= Url.Action("GetSlowData","Controller") %>',
            data: 'id=<%= ViewData["modelID"] %>',
            timeout: 15000,  // wait upto 15 secs
            success: function(content){
               $("#container").html(content);
            }
         });
     });
</script>

...

<div id="#container">
   Please wait while I retrieve the data.
</div>

Controller

public ActionResult ViewMyData( int id )
{
     ViewData["modelID"] = id;
     return View();
}

[AcceptVerbs( HttpVerbs.Post )]
public ActionResult GetSlowData( int id )
{
     var model = ... do what you need to do to get the model...

     return PartialView(model);
}

You'll also need a partial view (ViewUserControl) that takes your model and renders the view of the model. Note that this isn't complete -- you'll need to add error handling, you may want to consider what happens if javascript isn't enabled, ...

tvanfosson
I don't think so. The other solution requires me to return the data and then update my view with that data (using jQuery). I want to return a view (i.e. return View("MySlowData", myViewData) )
Simon Hutton
I'll update with an example.
tvanfosson
Thanks a lot, much clearer with an example. I wasn't aware you could render a partial view into an existing view (i.e. $("#container").html(content); ) but I suppose if the partial view is just returning Html why couldn't you! Thanks again.
Simon Hutton