Link to home
Start Free TrialLog in
Avatar of WorknHardr
WorknHardr

asked on

Ajax.BeginForm ActionResult Task Delay Goto?

I need to delay the ActionResult return Content until all the Tasks are completed. I like the way the Ajax.BeginForm uses the LoadingElementId to start the spinner.gif. The problem is the ActionResult returns before the Tasks are complete, so the spinner is useless.

[View]
   @using (Ajax.BeginForm("Import", "Books", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "result", LoadingElementId = "loading"}))
{
   ...
}

<div id="loading">
       <img src="~/Content/Images/spinner.gif" height="30" />
</div>

[Action]
    public async Task<ActionResult> Import(ImportViewModel model)
    {
           bool books = false; 

           foreach(....)
           {
                  books = await ImportBooks();
            }

           wait:

                Task.Delay(5000).Wait();

                if (books == false)
                {
                    goto wait;
                }
                else                    
                    return Content("Books import completed!");            
     }

     public async Task<bool> ImportBooks()
     {
          ....
          await context.SaveChangesAsync();

         return true;
     }

Open in new window

Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

You probably want to be looking at the AjaxOptions onComplete

http://msdn.microsoft.com/en-us/library/system.web.mvc.ajax.ajaxoptions.oncomplete%28v=vs.118%29.aspx

You create a handler for the onComplete event and disable your spinner there.
Avatar of WorknHardr
WorknHardr

ASKER

I have a problem with the Action completing before I want it to.
I've been looking into await Task.WhenAll(????) , unsure how to implement it so far..
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thx, I'll give that a go...
You are welcome - good luck with it.