I have tried different methods that have been posted on this site, but nothing seems to work. I want to create a clothing site (a personal project). The products on the site have their own class, that is built like this:
 public class Product
 {
      public string ProductName { get; set; }
      public string ProductPrice { get; set; }
      public int Quantity { get; set; }
 }
The shopping cart is another class that will contain a list of Product objects and this one is built like this:
public class ShoppingCart
{
    [Key]
    public int Id { get; set; }
    List<Product> ProductList { get; set; }
    public string ClientName { get; set; }
    public string ClientAddress { get; set; }
    public string ClientMail { get; set; }
}
I created an API Controller class and thought that would solve the problem. It looks like this:
[Route("api/Shopping")]
[ApiController]
public class ShoppingCartController : ControllerBase
{
    [HttpPost]
    public ShoppingCart Save([FromBody] ShoppingCart s)
    {
        return s;
    }
}
In my JavaScript code I create my JSON object and try to post it like this:
 var orderB = document.getElementById("orderB");
    orderB.addEventListener("click", function () {
        var inputName = document.getElementById("inputName").value;
        var inputAddress = document.getElementById("inputAddress").value;
        var inputMail = document.getElementById("inputMail").value;
        var auxArray = [];
        for (var i = 0; i < productsAux.length; i++) {
            auxArray[i] = { "productName": productsAux[i].titlu, "productPrice": productsAux[i].pret, "quantity": localStorage.getItem(productsAux[i].titlu)};
        }
        var shoppingCart = {
            productList: auxArray,
            clientName: inputName,
            clientAddress: inputAddress,
            clientMail: inputMail
        };
        $.ajax({
            type: "POST",
            data: JSON.stringify(shoppingCart),
            url: "api/shopping/save",
            contentType: "application/json charset=utf-8",
             }).done(function (res) {
            alert(res);
        });
       
After I push the order button on my page I expect to see the alert pop-up with the callback result which I suppose is the ShoppingCart object that is created using the JSON that I send.




[FromBody]. If action methods accept complex objects as parameter WebAPI uses registered MediaTypeFormatters for binding. Media type formatter for json content type is included by default in WebAPI."api/shopping. By default WebApi specifies calling method by HTTP Method type (GET, POST, etc.) , if no routing explicit specified.