Rogerio Azevedo Rogerio Azevedo - 6 months ago 9
JSON Question

Send two lists by Json and Get it from Array on JavaScript

I have two lists on my controller and I send that lists as ARRAY in a json to JavaScript.

See my Controller code here:

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

Its working fine and I can see two ARRAYS on JavaScript:

$.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);


Now I need...

Get data from list1 (controller1) and put it on 4 strings, like:

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


And the Second List/Array pass it to a HTML Table

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

{"aval":[{"Total":160,"Avalia1":25,"Avalia2":88.75,"Avalia3":73.13,"Avalia4":86.88}],"resumo":[{"Cod":"1195","Qtde":25,"Result":62},{"Cod":"1458","Qtde":15,"Result":73.33},{"Cod":"1722","Qtde":3,"Result":58.33},{"Cod":"2246","Qtde":5,"Result":65},{"Cod":"2509","Qtde":16,"Result":62.5},{"Cod":"2769","Qtde":3,"Result":100},{"Cod":"2918","Qtde":4,"Result":68.75},{"Cod":"3473","Qtde":9,"Result":66.67},{"Cod":"5044","Qtde":8,"Result":81.25},{"Cod":"5297","Qtde":11,"Result":65.91},{"Cod":"5463","Qtde":2,"Result":100},{"Cod":"5751","Qtde":4,"Result":75},{"Cod":"5967","Qtde":5,"Result":75},{"Cod":"6211","Qtde":7,"Result":60.71},{"Cod":"6558","Qtde":8,"Result":53.13},{"Cod":"7284","Qtde":2,"Result":75},{"Cod":"7939","Qtde":17,"Result":67.65},{"Cod":"7988","Qtde":16,"Result":76.56}]}

Answer

Based on the trincot's comment, I found the solution with a friend's help.

$.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);

            },