2
I am trying to pass a table via JSON.stringify, in ajax, is receiving the data perfectly, but I cannot pass to the controller. I’m passing that way:
function enviarDados(produtos) {
    $.ajax({
        method: "POST",
        url: '@Url.Action("Novo1","produtos")',
        contentType: 'application/json',
        dataType: 'json',
        data: JSON.stringify(produtos)
    });
}
However in the controller, I always get the list null, I’ve tried several ways, and none receives the values, always comes in null.
[HttpPost]
public IActionResult Novo1(List<PedidosProdutosF> produtos, NovoViewModel model)
{
}
I cannot pass JSON.stringify to controller in . NET CORE ?
EDIT
Class PedidosProdutosF:
[Key]
    public int Id { get; set; }
    public int ProdutoID { get; set; }
    public Produto Produto { get; set; }
    public string CodigoProduto { get; set; }
    [Display(Name = "Quantidade")]
    public int Qtd { get; set; }
    [Display(Name = "Preço Unitário")]
    public float PrecoUnitario { get; set; }
    [Display(Name = "Desconto em %")]
    public float DescontoP { get; set; }
    [Display(Name = "Desconto em R$")]
    public float DescontoV { get; set; }
    public int ICMS { get; set; }
    public float IPI { get; set; }
    public float ISS { get; set; }
    [Display(Name = "Data da Entrega")]
    public DateTime DataEntrega { get; set; }
    public int QtdFalta { get; set; }
    public float Total { get; set; }
    public float Aliquota { get; set; }
    public float VICMS { get; set; }
    public float VIPI { get; set; }
    public float VISS { get; set; }
    public int ? PedidoFornecedorId { get; set; }
    public PedidoFornecedor PedidoFornecedor { get; set; }
I made a console.log(JSON.stringify(produtos));
He returns the data to me, as follows:
[
  {
      "CodigoProduto":"P00062",
      "DescricaoProduto":"Descrição 62",
      "Qtd":"1",
      "PrecoCusto":"29,00",
      "DescontoP":"0,00",
      "DescontoV":"0,00",
      "Total":"29,00",
      "ICMS":"1",
      "AliquotaICMS":"0,00",
      "vICMS":"0,00",
      "ISS":"0,00",
      "vISS":"0,00",
      "IPI":"0,00",
      "vIPI":"0,00",
      "Qtdfalta":"",
      "ProdutoID":""
  }
]
Here is how I pass data from table to ajax, to be received in controller:
 $("#btn-enviar").click(function () {
    var produtos = [];
    $('.item').each(function () {
        var entidade = {
            CodigoProduto: $(this).children()[0].innerText,
            DescricaoProduto: $(this).children()[1].innerText,
            Qtd: $(this).children()[2].innerText,
            PrecoCusto: $(this).children()[3].innerText,
            DescontoP: $(this).children()[4].innerText,
            DescontoV: $(this).children()[5].innerText,
            Total: $(this).children()[6].innerText,
            ICMS: $(this).children()[7].innerText,
            AliquotaICMS: $(this).children()[8].innerText,
            vICMS: $(this).children()[9].innerText,
            ISS: $(this).children()[10].innerText,
            vISS: $(this).children()[11].innerText,
            IPI: $(this).children()[12].innerText,
            vIPI: $(this).children()[13].innerText,
            Qtdfalta: $(this).children()[15].innerText,
            ProdutoID: $(this).children()[16].innerText,
        };
           produtos.push(entidade);
    });
    enviarDados(produtos);
});
You tried to send only the
data: produtos? can add an example of the generated Json and its Request class?– Barbetta
I tried data:products, but it returns error, updated the question with the class, and the Json example.
– Mariana
I can’t do some Agra tests, but I found this in the OS, it might help you: https://stackoverflow.com/questions/13779043/post-json-array-to-mvc-controller
– Barbetta
I’m doing tests with these examples, but so far, no success.
– Mariana
I edited with the part that I send the table data to ajax.
– Mariana
@marianac_costa, you posted a console print.log(products), put the pure TXT so that I can simulate here inside my Javascript?
– Leonardo Bonetti
[{"CodigoProduto":"P00062","DescricaoProduto":"Descrição 62","Qtd":"1","PrecoCusto":"29,00","DescontoP":"0,00","DescontoV":"0,00","Total":"29,00","ICMS":"1","AliquotaICMS":"0,00","vICMS":"0,00","ISS":"0,00","vISS":"0,00","IPI":"0,00","vIPI":"0,00","Qtdfalta":"","ProdutoID":""}]
– Mariana
@Leonardobonetti is there.
– Mariana
@marianac_costa did tests on ASP.NET MVC 4 and 5 and worked perfectly, but I did the test by passing the direct string, does so for test effect, when you create the variable
produtosattribute to it this valuevar produtos = [{ "CodigoProduto": "P00062", "DescricaoProduto": "Descrição 62", "Qtd": "1", "PrecoCusto": "29,00", "DescontoP": "0,00", "DescontoV": "0,00", "Total": "29,00", "ICMS": "1", "AliquotaICMS": "0,00", "vICMS": "0,00", "ISS": "0,00", "vISS": "0,00", "IPI": "0,00", "vIPI": "0,00", "Qtdfalta": "", "ProdutoID": "" }];and test by sending AJAX– Leonardo Bonetti
I did the test and it didn’t work, will the problem is in CORE ?
– Mariana
I think so Martian, I’ll check if there’s anything related
– Leonardo Bonetti
@marianac_costa atlera
List<PedidosProdutosF> produtosfor[FromBody]List<PedidosProdutosF> produtos– Leonardo Bonetti
It returns error, This page is not working If the problem continues, please contact the owner of the site.
– Mariana