Simon Hutton
Simon Hutton

Reputation: 1374

Loading Page for ASP.Net MVC

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

Upvotes: 1

Views: 8385

Answers (1)

tvanfosson
tvanfosson

Reputation: 532755

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

Upvotes: 5

Related Questions