passing a complex object to MVC4 controller

Asked

Viewed 45 times

1

I’m using MVC4 C#, I have a table that is generated dynamically 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 climb through ajax of all listed objects plus some objects that are not in the table, as payment method;

follows the js that I’ve assembled 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 refer to the product I want to assemble through the table, does anyone have any solution to my problem? and how I receive this value in Controller?

  • Do you have any form? then it would be enough form.serialize();?

  • I do, but since this list is being created dynamically, the generated object is going null to the Controller

2 answers

1


Put the list in json format

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,        
      }]
}
  • but I do not know how many products were selected by the customer, as I will pass "id_product": i

0

I found a solution, I don’t 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 don’t know yet how I’m going to treat it in the controller, but because I haven’t tested it yet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.