Write a Json string to the database c#

Asked

Viewed 389 times

0

I am able to receive the data correctly, and being able to separate them correctly too, only problem is that I do not know how to use the values and be able to write in the database, could help me ?

The image below shows how I am receiving the data and treating them until then:

inserir a descrição da imagem aqui

[HttpPost]
    public async Task<IActionResult> SellAdd(string products)
    {
        var objects = JsonConvert.DeserializeObject<List<object>>(products);
        string[] model = objects.Select(x => x.ToString()).ToArray();


        foreach (var product in model)
        {

        //    var produtoCodigo = await _productManager.GetProductCodigoAsync();

          /*  var venda = new ApplicationSell
            {
                Quantidade = product.quantidadeProduto,
                Total = product.valorTotalProduto,
                ProductId = produtoCodigo.Codigo
            };

            await _productManager.GravaVendaAsync(venda);  */
        }

        return RedirectToAction("Index");
    }
  • Hello @Matheus. Avoid putting your code in images, instead edit your question and place it there.

  • have any code where you are recording something in the database? Or still have nothing related to it?

1 answer

1


Hello, in my humble opinion, you could use the Entity framework for database access.

Would create a class:

public class Products {
    int    codigoProduto {get;set;}
    int    quantidadeProduto {get;set;}
    double valorTotalProduto {get;set;}
}

Products  prod = JsonConvert.DeserializeObject<Products>(products);

Just below you could access the properties prod.valorTotalProduto for example.

Then use the Entity framework to write ,update or delete in the database.

I hope I’ve helped myself.

  • But in this case class products has to have exactly the same json name in the case ?

  • Yes, because you’re making an association.

Browser other questions tagged

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