Solved

Ajax.BeginForm ActionResult Task Delay Goto?

Posted on 2014-10-23
5
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 57

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 57

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 57

Expert Comment

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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

734 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