0


I'm working on a tool in MVC. When I upload a file I need to take some xls variables out from it and insert them to the Database. This could take 30-40 sec. I would like to show some feedback to the user while this long process is executed. So I added a function named GetImportStatus() which should return the prcessed variables to the user. I add this function to the Controller that executes the insertion in Database. So my controller looks like

 public class AdminController : Controller
 {
    int currentIndex;
    int totalVariables;

public ActionResult GetAllVariables()
{
    //processing code here
    totalVariables = Variables.Count;   
}

//other stuffs here

public void InserVariablestToDatabase()
{
    foreach(string variable in VariableList)
    {
        Insert(variable);
        currentIndex++;
    }
}

//other code here

    public JsonResult GetImportStatus()
    {
        return Json((new { Current = currentIndex.ToString(), Total = totalVariables.ToString() }), JsonRequestBehavior.AllowGet);

    }
}

My view has this part,

<div id="updateDiv"></div>
<script type="text/javascript">
    function updateScreen() {
        $.ajax({
            type: 'GET',
            url: '@Url.Action("GetImportStatus","Admin")',
            content: "application/json; charset=utf-8",
            success: function (data) {
                $("#updateDiv").append("<strong>" + data + "</strong>");
            }
        });
    }

    $("#btnUploadFile").click(function ()
    { var id = setInterval(updateScreen, 1000); });

</script>

where "btnUploadFile" is the upload button. When I inspect the .NET panel from firebug, I see the requests but I get no responses until the importing process is finished. See image below
enter image description here

Am I doing something wrong?
Thank You! :)

tereško
  • 58,060
  • 25
  • 98
  • 150
Coder
  • 886
  • 1
  • 12
  • 28
  • have you made debug statement to make sure that call is correct? – Ashwini Verma Apr 17 '14 at 06:09
  • yes, but is not called until the insert operation is finished – Coder Apr 17 '14 at 06:12
  • It just a view, could you add in your `ajax` function `async:false,` after `content` –  Apr 17 '14 at 06:20
  • same thing is happening :( – Coder Apr 17 '14 at 06:30
  • I added a breakpoint to the json response and a breakpoint to the last line of the inserting process. The GetImportStatus is called after the Insert process is finished. But I can see the requests from the firebug... – Coder Apr 17 '14 at 06:38

1 Answers1

1

I think as per your scenario you need to have async methods.

private async void InserVariablestToDatabase()
{
     ....
}

public async Task<JsonResult> GetImportStatus()
{
     ....
}

You would have worth reading this.

Community
  • 1
  • 1
Ashwini Verma
  • 7,477
  • 6
  • 36
  • 56
  • It's a little bit embarrassing that for things that look simple you have to do sooo complicated stuffs in the back end. Thanks for your help :) – Coder Apr 25 '14 at 12:55