0

I have a function that take few seconds to be completed. after completing the task in a separate thread, I want to load the Result view to the browser. Until that I want to show a loading gif in the browser.

This is how my controller method

public ActionResult evaluateBulk(string year, string exam, string centre, string course, string batch)
    {
        string path = Directory.GetCurrentDirectory() + @"\" + year + @"\" + exam + @"\" + centre + @"\" + course + @"\" + batch + @"\";
        string[] listOfImages = getAllFiles(path);

        var th = new Thread(processImages);
        th.Start(listOfImages);
        th.Join();

        return View("Result");
    }

This is how the above controller method is called from the View

        function evaluateBulk() {

        var year = $("#year option:selected").val();
        var exam = $("#exam option:selected").val();
        var centre = $("#centre option:selected").val();
        var course = $("#course option:selected").val();
        var batch = $("#batch option:selected").val();

        $.ajax({
            async: true,
            url: "evaluateBulk",
            data: { year: year, exam: exam, centre: centre, course: course, batch: batch },
            cache: false,
            dataType: "json",
            success: function (data) {
                alert("Done");
            },
            error: function (xhr, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });
    }

How to integrate a loading gif into this view to display until the Result view is loaded with the results of the method..

Thanks

jrummell
  • 42,637
  • 17
  • 112
  • 171
Chamith
  • 129
  • 1
  • 13
  • 1
    Isn't the answer in the question? Show the image when you make the ajax query, and hide it when it completes.. – stuartd Jun 15 '17 at 13:14

1 Answers1

2

Add a loader image to your code

<div id="loadingImg" style="display:none;">
 <img src="loading.gif"/>
</div>

Modify your ajax code

  $('#loadingImg').show();
     $.ajax({
        async: true,
        url: "evaluateBulk",
        data: { year: year, exam: exam, centre: centre, course: course, batch: batch },
        cache: false,
        dataType: "json",
        success: function (data) {
            alert("Done");
        },
      complete: function(){
      $('#loadingImg').hide();
      },
        error: function (xhr, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
Mohan Singh
  • 1,142
  • 3
  • 15
  • 30