Solved

Ajax.BeginForm ActionResult Task Delay Goto?

Posted on 2014-10-23
5
284 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 51

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 51

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 51

Expert Comment

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Get String split 5 33
Jquery autocomplete 10 27
Cannot upload files above 1mb IIS7 11 15
Saveas need to save a copy 16 11
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now