passing a complex object to MVC4 controller

1

I'm using MVC4 C #, I have a table that is dynamically generated according to the user's decision, in this format:  generated by js

<tr class="itemPedido"> ' +
 '<td class="col-sm-1">' +
'<p class="codigo">' + $(this).attr('data-codigo') + '</p>' +
'</td>' +
 '<td class="col-sm-4">' +
'<p class="desc">' + $(this).attr('data-desc') + '</p>' +
'</td>' +
 '<td class="col-sm-1">' +
'<p class="un">' + $(this).attr('data-un') + '</p>' +
'</td>' +
'<td class="col-sm-1">' +
'<p class=grupo">' + $(this).attr('data-grupo') + '</p>' +
'</td>' +
'<td class="col-sm-1">' +
'<input class="form-control qnt" step ="0.01" type="number" placeholder="' + parseFloat($(this).attr('data-qnt').replace(',','.')) + '" data-max="' + $(this).attr('data-qnt') + '"' +
'</td>' +
'<td class="col-sm-1">' +
'<input class="form-control preco"step="0.01" type="number" value="'+parseFloat($(this).attr('data-preco').replace(',','.'))+'"' +
'</td>' +
'<td class="col-sm-1 total">' +"R$ 0.00"+
'</td>' +
'</tr>'

What I want is to generate an object to go up by ajax of all listed objects plus some objects that are not in the table as a form of payment;

Follows the js I've mounted so far

var data = {
  date : $('#Data').val(),
  clienteNo: $('#ClienteNO').val(),
  clienteRaz: $('#ClienteRazao').val(),
  entrega: $('#Entrega').val(),
  formapg: $("#FormaPg").val(),
  tipopg: $('#tipopg').val(),
  frete: $('#frete').val(),
  processo: $('#Processo').val(),
  obs: $('#Obs').val(),
  traNum: $('#TraNumero').val(),
  traRaz: $('#TraRazao').val(),
  traTel: $('#TraTel').val(),
  produtos: /*?????*/
}

The question marks are referring to the product I want to assemble through the table, does anyone have any solutions to my problem? and how do I get this value in Controller ?

    
asked by anonymous 25.11.2016 / 16:55

2 answers

1

Put json in the list

And in the controller you will receive an array of Products or a list ...

var data = {
  date : $('#Data').val(),
  clienteNo: $('#ClienteNO').val(),
  clienteRaz: $('#ClienteRazao').val(),
  entrega: $('#Entrega').val(),
  formapg: $("#FormaPg").val(),
  tipopg: $('#tipopg').val(),
  frete: $('#frete').val(),
  processo: $('#Processo').val(),
  obs: $('#Obs').val(),
  traNum: $('#TraNumero').val(),
  traRaz: $('#TraRazao').val(),
  traTel: $('#TraTel').val(),
  produtos:[
      {
        "id_produto": 1,
      },
      {
        "id_produto": 2,        
      }]
}
    
25.11.2016 / 17:09
0

I found a solution, I do not know if it's the best:

 $('.itemPedido').each(function () {
                    i++;
                    var prod = {
                        codigo: $(this).find('.codigo').text(),
                        descricao: $(this).find('.desc').text(),
                        un: $(this).find('.un').text(),
                        grupo: $(this).find('.grupo').text(),
                        quantidade: $(this).find('.qnt').val(),
                        preco: $(this).find('.preco').val()
                    }
                    prdt.push(prod);
                })
                var data = {
                    date: $('#Data').val(),
                    clienteNo: $('#ClienteNO').val(),
                    clienteRaz: $('#ClienteRazao').val(),
                    entrega: $('#Entrega').val(),
                    formapg: $("#FormaPg").val(),
                    tipopg: $('#tipopg').val(),
                    frete: $('#frete').val(),
                    processo: $('#Processo').val(),
                    obs: $('#Obs').val(),
                    traNum: $('#TraNumero').val(),
                    traRaz: $('#TraRazao').val(),
                    traTel: $('#TraTel').val(),
                    produtos: prdt
                }
            })

I just do not know how I'm going to handle the controller, but I have not tested it yet.

    
25.11.2016 / 17:34