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

1
  • 1
    I can't see of any way to do this without using AJAX - I know it isn't what you want to hear but ASP.Net won't send any part of the response until the processing is complete on the server. Whereas jQuery AJAX will be able to do this for you after the response has been sent. Commented Feb 18, 2009 at 22:16

1 Answer 1

5

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, ...

Sign up to request clarification or add additional context in comments.

2 Comments

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) )
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.