Reputation: 780
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
}
Upvotes: 0
Views: 330
Reputation: 3393
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
}
Upvotes: 1