Pass JSON.stringify to Controller

1

I'm trying to pass a table via JSON.stringify, in ajax, it's receiving the data perfectly, but I can not get into the controller. I'm passing this way:

function enviarDados(produtos) {

    $.ajax({
        method: "POST",
        url: '@Url.Action("Novo1","produtos")',
        contentType: 'application/json',
        dataType: 'json',
        data: JSON.stringify(produtos)

    });
}

But in the controller, I always get the list null, I've already tried it in several ways, and none of it gets the values, it always comes in null.

[HttpPost]
public IActionResult Novo1(List<PedidosProdutosF> produtos, NovoViewModel model)
{
}

I can not 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));

It returns me the data, 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 the table data to ajax, to be received in the 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);
});
    
asked by anonymous 23.08.2018 / 14:48

2 answers

2

Check your model data with the data being passed in your JSON. You must pass values to all fields that do not accept null in your model. Another point is that your JSON has fields that have different model names, for example, the AliquotaICMS field in JSON has the name Aliquota in the model.

The data types must also be respected when passing the Json object in the Ajax request, so use parseInt , parseFloat when mounting your object.

$("#btn-enviar").click(function () {
    var produtos = [];

    $('.item').each(function () {
        var entidade = {
            CodigoProduto: $(this).children()[0].innerText,
            //Não existe o campo DescricaoProduto na model, então retirei do Json
            //DescricaoProduto: $(this).children()[1].innerText,
            Qtd: parseInt($(this).children()[2].innerText),
            PrecoUnitario: parseFloat($(this).children()[3].innerText),
            DescontoP: parseFloat($(this).children()[4].innerText),
            DescontoV: parseFloat($(this).children()[5].innerText),
            Total: parseFloat($(this).children()[6].innerText),
            ICMS: parseInt($(this).children()[7].innerText),
            Aliquota: parseFloat($(this).children()[8].innerText),
            vICMS: parseFloat($(this).children()[9].innerText),
            ISS: parseFloat($(this).children()[10].innerText),
            vISS: parseFloat($(this).children()[11].innerText),
            IPI: parseFloat($(this).children()[12].innerText),
            vIPI: parseFloat($(this).children()[13].innerText),
            Qtdfalta: parseInt($(this).children()[15].innerText),
            ProdutoID: parseInt($(this).children()[16].innerText),
            DataEntrega: '2018-08-25',
            Id: 1
        };

           produtos.push(entidade);
    });

    enviarDados(produtos);
});

In your Controller add [FromBody] before your product list, to inform that these values are coming from the request body POST :

[HttpPost]
public IActionResult Novo1([FromBody]List<PedidosProdutosF> produtos)
    
23.08.2018 / 15:25
0

You are mistaken in two points, in the name of some attributes and in the type of them, for example you are sending in your JSON the vICMS, but in your model this ICMS, and in the case of all attributes float and% you should use int and parseFloat respectively before sending JSON, so you will not have problems, and the parseInt parameter of your method is not being used and you should remove it.

    
24.08.2018 / 19:12