Click here to Skip to main content
16,014,734 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working in ASP.NET MVC2 project. But I have faced a problem that when I call and get asynchronus data from controller that would come after execution of my next statements.
JavaScript
//Jquery part------------

//this folowing method is not working properly for population SET_QualityTestCollection,
//but if i check it after execution(3-5 second delay) this mehod i am getting  data for   SET_QualityTestCollection
$(document).ready(function () {
    var collection = new GetCollectionYS_POTestItemForGrid(); 
});
var SET_QualityTestCollection = [];


function GetCollectionYS_POTestItemForGrid() {
    this.InsertUser = $("#ViewYS_POTestItem_YS_POTestItem_InsertUser").val() == null ? "" : $("#ViewYS_POTestItem_YS_POTestItem_InsertUser").val();
    this.Test = "<select id=\"ddlTest_" + YS_POTestItemCurrentID + "\" class = \"ddlQualityTest\"  önchange=\"TestRequirementsByTestId(" + YS_POTestItemCurrentID + ")\">" + SetCollectionForDdl() + "</select>";
    this.TestRequirment = "<select id=\"ddlRequirements_" + YS_POTestItemCurrentID + "\" class = \"ddlTestRequirements\"><option value='" + 0 + "' \selected=\"selected\">...Select Requirements...</option></select>";
    this.TestMethod = "<select id=\"ddlTstMethod_" + YS_POTestItemCurrentID + "\" class = \"ddlTestMethod\">" + SetCollectionForTestMethodDdl(GetTestMethod()) + "</select>";
    this.rownumber = YS_POTestItemCurrentID.toString();
    this.Tag = 1;
    this.Edit = "<a style="\"text-decoration:" mode="hold" />    this.Delete = "<a style="\"text-decoration:" mode="hold" />}
function SetCollectionForDdl() {
    var datacol = GetQualityTest();
    var s = '<option value="0" selected="selected">...Select...</option>';
    for (var i = 0; i < datacol.length; i++) {
        s += '<option value="' + datacol[i].id + '">' + datacol[i].TestName + '</option>';
    }
        return s;  
}

function GetQualityTest() {
    if (SET_QualityTestCollection.length == 0) {
      $.post("/YS_PO/GetQualityTest", { },
               function (data, textStatus) {
                   if (textStatus == "success") {
                       SET_QualityTestCollection = data;
                   }
               },"json");
        }
        return SET_QualityTestCollection;
}


//controller 
 public ActionResult GetQualityTest()
        {
            try
            {
               Bll.QualityTestListinfo qualityTestListinfo = new Bll.QualityTestListinfo();
               return Json(qualityTestListinfo.GetDatas(), JsonRequestBehavior.AllowGet);
        }
Posted
Updated 22-Jan-11 21:54pm
v2
Comments
Maxima Prince 23-Jan-11 3:58am    
yes i am having the same problem :((
Manfred Rudolf Bihy 23-Jan-11 9:30am    
I think I found out OP's and your problem is. Check out my answer below. Cheers!

1 solution

The problem you're facing is that you are calling SetCollectionForDdl() synchronously which means that this function returns as soon as it has exectuted. In function SetCollectionForDdl() you are the calling function GetQualityTest() also synchronously with the same implications as before: It returns immediately after it sent the asynchronous AJAX call.
Since your AJAX call takes some time to complete the data has not been fetched yet. I suggest you try it like this:

$(document).ready(function () {
    // First thing after the page is fully loaded let us fetch
    // the data with our AJAX request
    GetQualityTest();
});

function GetQualityTest() {
    if (SET_QualityTestCollection.length == 0) {
        $.post("/YS_PO/GetQualityTest", { },
               function (data, textStatus) {
                   if (textStatus == "success") {
                       SET_QualityTestCollection = data;
                       // now call the code where your data is rendered to HTML
                       // because data will now be available
                       doHTMLRenderingStuff();
                   }
               },"json");
    }
}

function doHTMLRenderingStuff()
{
    // Here you would do all the stuff that your two functions do.
    // When this function has gotten called via the AJAX callback you can
    // rely on the SET_QualityTestCollection variable to hold the 
    // proper data returned from you AJAX call. 
}


Hope this helps you get the idea. I didn't want to refactor the two functions for you as I think you'd be better at doing that :). I just needed to point out that mixing asynchronous calls with synchronous calls doesn't yield the expected results.

Best Regards,
Manred
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900