0

It's my first time to pass an array to ajax using JSON object. As you can see with my code, I'm not sure how to properly pass an array to a JSON object to be read by ajax. When it got passed to the Controller, the items variable in the parameter is empty.

View Javasript

var itemprice = [];
//Populate itemprice. This will be used to check if the newly added item is already existing
$('#tblItem tbody tr td:nth-child(5)').each(function () {
    itemprice.push($(this).text());
});

var json = {
    item: $('#item').val(),
    itemtypeid: $('#itemtype option:selected').val(),
    itempromocount: $('#tblItem tbody tr #tditem_promo').length,
    items: itemprice //I'm not sure how to pass an array to Controller
};

$.ajax({
    url: '/Items/CheckItems',
    type: 'POST',
    dataType: 'json',
    data: JSON.stringify(json),
    contentType: 'application/json; charset=utf-8',
    cache: false,
    async: true,
    success: function (response) {
        ...
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert("Error! " + xhr.status);
    }
});

Controller

public JsonResult CheckItems(string itemtypeid, int itempromocount, string items)
{
    //itemtypeid and itempromocount has value but items don't have
}

1 Answer 1

1

in your controller items is a list of string :

public JsonResult CheckItems(string itemtypeid, int itempromocount, List<string> items)
{
    //itemtypeid and itempromocount has value but items don't have
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh God why! I feel so stupid haha. Thanks for pointing that out, man! I thought it was how I pass the array to the JSON object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.