Solved

Ajax.BeginForm ActionResult Task Delay Goto?

Posted on 2014-10-23
5
289 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 53

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 53

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 53

Expert Comment

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

23 Experts available now in Live!

Get 1:1 Help Now