0
JSONオブジェクトを使ってajaxに配列を渡すのは初めてのことです。私のコードでわかるように、AJAXによって読み込まれるJSONオブジェクトに配列を正しく渡す方法がわかりません。コントローラーに渡されたとき、パラメーターの変数items
は空です。配列をJSONオブジェクトに渡し、他の変数と一緒にajaxで使用する
ビュー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);
}
});
コントローラ
public JsonResult CheckItems(string itemtypeid, int itempromocount, string items)
{
//itemtypeid and itempromocount has value but items don't have
}
ああ、神様、なぜ!私はとても馬鹿だと思う。それを指摘してくれてありがとう、男!私はそれがJSONオブジェクトに配列を渡す方法だと思った –