Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a requirement like, i have to make multiple jquery ajax calls and bind the data to multiple divs in my view.

The below is the code in my main view

@foreach (var item in Model)
   {
      <p>Chart name : @item.ChartName</p>
       <div>
           @{Html.RenderAction("GetChartData", "ChartsDashboard", new { Data = item });}
       </div>
       <br/>
   }


RenderAction will call another view whose code is like this :

XML
<script>
//dynamically adding the divs based on the for loop
    var chrtdivid = "chrt"+ '@Model[0].ChartId.ToString()'
    $("#charts").append("<div id='" + chrtdivid + "'>hello world</div>")

    $(function () {
        $.ajax({
            type: "GET",
            url: "/api/ChartsAPI/" + '@Model[0].ChartId.ToString()',
            dataType: "json",
            success: function (seriesData) {

                //bind this series data to the dynamically added div
            }});});
    </script>



So in this requirement based on the foreach list value, there can be n no.of loops, so n no.of calls will be made to the jquery ajax.
But finally only the last div is bound with the data, but not all.

Is there any way to get the data to the divs on every web api call?
Posted
Comments
Sergey Alexandrovich Kryukov 21-Jul-15 9:19am    
I see no problem here. If you can populate one div with data, why can't you do it with a set of divs? When you do N similar Ajax calls with different div for each iteration, it will asynchronously update each separate div on each success.
Do you mean that you just have some bug, so the data from all loop iterations go to one div? But then you should show your code now commented "//bind this series data to the dynamically added div".
—SA
goshan2011 21-Jul-15 23:40pm    
in the commented section i am binding the web api data to a telerik chart and i am maintaining separate ids for each chart. The problem is that when i have a data of 3 charts to bind and display, the first div chart never loads, the second div chart loads the data of the first and the third with the third. Some times only the 3rd div chart loads with the first data and the other 2 never loads. What's going wrong, you have any idea?
Sergey Alexandrovich Kryukov 22-Jul-15 0:54am    
Still, you did not provide the code in this part. I suggest you use the debugger. I suspect you are updating the same HTML element, instead of separate ones.
—SA

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