通过Json发送两个列表并从JavaScript上获取它

时间:2016-05-28 20:03:23

标签: javascript arrays json

我的控制器上有两个列表,我将该列表作为ARRAY发送到json中的JavaScript。

请在此处查看我的控制器代码:

var aval = new List<AvaliacaoViewModel>();
aval = relData.GetAvaliacao(data_1, data_2, cliente, operador);

var resumo = new List<ResumoViewModel>();
resumo = relData.GetResumo(data_1, data_2, cliente, operador);               

var result = new { aval = aval, resumo = resumo };
return Json(result, JsonRequestBehavior.AllowGet);

My list1 - Controller-1

My list2 - COntroller-2

它工作正常,我可以在JavaScript上看到两个ARRAYS:

$.ajax({
            url: '/Relatorios/AvalOperador',
            dataType: "json",
            type: "GET",
            data: { 'data1': data1, 'data2': data2, 'operador': operador },
            success: function (data) {                   

            debugger;

            var aval1 = avalia.getValue(1);
            var aval2 = avalia.getValue(2);
            var aval3 = avalia.getValue(3);
            var aval4 = avalia.getValue(4);

现在我需要......

从list1(controller1)获取数据并将其放在4个字符串上,如:

var avalia1 = column[1].toString();
var avalia1 = column[2].toString();
var avalia1 = column[3].toString();
var avalia1 = column[4].toString();

第二个列表/数组将其传递给HTML表

 <table class="table table-striped">
     <thead>
         <tr>
             <th>Cod</th>
             <th>Operador</th>
             <th>Qtde</th>
             <th>Pie </th>
         <th>Status</th>

         </tr>
     </thead>
     <tbody>
         <tr>
             <td>3120</td>                                           
             <td>Patrick Smith</td>
             <td>2</td>                                           
             <td><span class="pie">85/100</span></td>
             <td>85%</td>                                                                                   
         </tr>                                                                   
     </tbody>
 </table>

Console Stringfy:

{&#34; AVAL&#34;:[{&#34;总计&#34;:160,&#34; Avalia1&#34;:25,&#34; Avalia2&#34;:88.75&# 34; Avalia3&#34;:73.13&#34; Avalia4&#34;:86.88}],&#34; resumo&#34;:[{&#34;鳕鱼&#34;:&#34; 1195&#34; &#34; Qtde&#34;:25,&#34;结果&#34; 62},{&#34;鳕鱼&#34;:&#34; 1458&#34;&#34; Qtde&#34 ;:15,&#34;结果&#34;:73.33},{&#34;鳕鱼&#34;:&#34; 1722&#34;&#34; Qtde&#34;:3,&#34;结果&#34;:58.33},{&#34;鳕鱼&#34;:&#34; 2246&#34;&#34; Qtde&#34;:5,&#34;结果&#34; 65}, {&#34;鳕鱼&#34;:&#34; 2509&#34;&#34; Qtde&#34;:16,&#34;结果&#34;:62.5},{&#34;鳕鱼&#34 ;:&#34; 2769&#34;,&#34; Qtde和#34;:3,&#34;结果&#34;:100},{&#34;鳕鱼&#34;:&#34; 2918&# 34;,&#34; Qtde&#34:4,&#34;结果&#34;:68.75},{&#34;鳕鱼&#34;:&#34; 3473&#34;&#34; Qtde& #34;:9,&#34;结果&#34;:66.67},{&#34;鳕鱼&#34;:&#34; 5044&#34;&#34; Qtde&#34;:8,&# 34;结果&#34;:81.25},{&#34;鳕鱼&#34;:&#34; 5297&#34;&#34; Qtde&#34;:11,&#34;结果&#34;:65.91 },{&#34;鳕鱼&#34;:&#34; 5463&#34;&#34; Qtde&#34;:2&#34;结果&#34;:100},{&#34;鳕鱼& #34;:&#34; 5751&#34;&#34; Qtde&#34:4,&#34;结果&#34; 75},{&#3 4;鳕鱼&#34;:&#34; 5967&#34;&#34; Qtde&#34;:5,和#34;结果&#34; 75},{&#34;鳕鱼&#34;:& #34; 6211&#34;&#34; Qtde&#34;:7,&#34;结果&#34;:60.71},{&#34;鳕鱼&#34;:&#34; 6558&#34 ;, &#34; Qtde&#34;:8,&#34;结果&#34;:53.13},{&#34;鳕鱼&#34;:&#34; 7284&#34;&#34; Qtde&#34; :2,&#34;结果&#34; 75},{&#34;鳕鱼&#34;:&#34; 7939&#34;&#34; Qtde&#34;:17,&#34;结果& #34;:67.65},{&#34;鳕鱼&#34;:&#34; 7988&#34;&#34; Qtde&#34;:16,&#34;结果&#34;:76.56}]}

1 个答案:

答案 0 :(得分:1)

根据trincot的评论,我在朋友的帮助下找到了解决方案。

$.ajax({
            url: '/Relatorios/AvalOperador',
            dataType: "json",
            type: "GET",
            data: { 'data1': data1, 'data2': data2, 'operador': operador },
            success: function (data) {

                var aval1 = JSON.stringify(data.aval[0].Avalia1);
                var aval2 = JSON.stringify(data.aval[0].Avalia2);
                var aval3 = JSON.stringify(data.aval[0].Avalia3);
                var aval4 = JSON.stringify(data.aval[0].Avalia4);

                var resumo = "";

                for (var i in data.resumo) {
                    resumo += "<tr>" +
                        "<td>" + data.resumo[i].Cod + "</td>" +
                        "<td>" + data.resumo[i].Qtde + "</td>" +
                        "<td>" + data.resumo[i].Result + "</td>" +
                        "</tr>";
                }               

                $('#lbAvalia1').html(aval1);
                $('#lbAvalia2').html(aval2);
                $('#lbAvalia3').html(aval3);
                $('#lbAvalia4').html(aval4);
                $("#redumoBody").html(resumo);

            },