Search code examples
c#asp.net-mvcsimplecart

What format does simplecart POST data in? failing to get the POST data in MVC project


I have run into a wall, how does simplecart POST data? I have tried taking in a bunch of different data types and all I get is null.

JS for the simplecart setup:

<script>
simpleCart({
currency: "AUD" // set the currency to pounds sterling
});

simpleCart({
    cartColumns: [
    { attr: "name" , label: "Name" } ,
    { attr: "size", label: "Size"},
    { attr: "price" , label: "Price", view: 'currency' } ,
    { view: "decrement" , label: false , text: "-" } ,
    { attr: "quantity" , label: "Qty" } ,
    { view: "increment" , label: false , text: "+" } ,
    { attr: "total" , label: "SubTotal", view: 'currency' } ,
    { view: "remove" , text: "Remove" , label: false }
    ]
});

simpleCart({
    checkout: {
        type: "SendForm",
        url: "umbraco/surface/cart/cart"
        }
    });
</script>

My MVC Controller:

// POST: cart
    [HttpPost]
    public ActionResult cart(string contents)
    {
        var test = JsonConvert.DeserializeObject(contents);
        return null;
    }

Does anybody know how to fix this so it actually reads into the controller? I have tried making a model with the same data as the cart and still got null.


Solution

  • I just needed a list of the variables that are sent by simplecart so I could use my controller properly.

    I used a get call and then a Request.Form call to find out the variables sent by SimpleCart.

    Turns out I will need to use the request method mentioned above as simplecart just keeps tacking the items and all their details onto the end rather than using a JSON string or anything.

    list is as follows, please note that items 6+ are repeated for as many items as you have:

    1. currency
    2. shipping
    3. tax
    4. taxRate
    5. itemCount
    6. item_name_1
    7. item_quantity_1
    8. item_price_1
    9. item_options_1

    I have answered my own question for the benefit of anyone else using simplecart.js in mvc, if anyone has a more elegant solution than using the request to get a variable number of post entries please post.