Solved

Ajax.BeginForm ActionResult Task Delay Goto?

Posted on 2014-10-23
5
308 Views
Last Modified: 2014-10-27
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

0
Comment
Question by:WorknHardr
  • 3
  • 2
5 Comments
 
LVL 56

Expert Comment

by:Julian Hansen
ID: 40403066
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.
0
 

Author Comment

by:WorknHardr
ID: 40403159
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..
0
 
LVL 56

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 40406058
That is why I suggested the onComplete - this is called when the AJAX call is completed - have you tried implementing an onComplete handler?
Alternatively look at onSuccess

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

Open in new window



Inside the ProcessHere method - implement the code you want to run after call completed.
0
 

Author Closing Comment

by:WorknHardr
ID: 40406223
Thx, I'll give that a go...
0
 
LVL 56

Expert Comment

by:Julian Hansen
ID: 40406286
You are welcome - good luck with it.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question