如何在视图上调用多个ajax调用? [英] How to call multiple ajax calls on a view?

查看:62
本文介绍了如何在视图上调用多个ajax调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,我必须在我的视图中进行多个jquery ajax调用并将数据绑定到多个div。



以下是代码我的主要观点



 @foreach(模型中的var项目)
{
< p>图表名称:@ item.ChartName< / p为H.
< div>
@ {Html.RenderAction(GetChartData,ChartsDashboard,new {Data = item});}
< / div>
< br />
}





RenderAction将调用另一个代码如下的视图:



 <   script  >  
//根据for循环动态添加div
var chrtdivid =chrt+'@ Model [0] .ChartId.ToString ()'
$(#charts)。append(< div < span class =code-attribute> id =' + chrtdivid +' > hello world < / div >

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

//将此系列数据绑定到动态添加的div
}});});
< / script >







所以在这个基于foreach列表值的要求中,可以有n个。循环,所以n no.of将调用jquery ajax。

但最后只有最后一个div与数据绑定,但不是全部。



有没有办法在每次网络api调用时将数据传递给div?

解决方案

(#charts)。append( < div id =' + chrtdivid +' > hello world < / div >

(本功能ion(){


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

//将此系列数据绑定到动态添加的div
}});});
< / script >







所以在这个基于foreach列表值的要求中,可以有n个。循环,所以n no.of将调用jquery ajax。

但最后只有最后一个div与数据绑定,但不是全部。



有没有办法在每次网络API呼叫时将数据传送给div?


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 :

<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?

解决方案

("#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?


这篇关于如何在视图上调用多个ajax调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆