2016-12-27 1 views
0

Es ist das erste Mal, ein Array an Ajax mit JSON-Objekt übergeben. Wie Sie mit meinem Code sehen können, bin ich mir nicht sicher, wie man ein Array an ein JSON-Objekt übergeben soll, das von Ajax gelesen werden soll. Wenn es an den Controller übergeben wurde, ist die Variable items im Parameter leer.Übergeben Array zu JSON-Objekt zusammen mit anderen Variablen in Ajax verwendet werden

Ansicht 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 
} 

Antwort

1

in Ihrem Controller items ist eine Liste von string:

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

Oh Gott, warum! Ich fühle mich so dumm haha. Danke, dass du das gezeigt hast, Mann! Ich dachte, es ist, wie ich das Array an das JSON-Objekt übergeben –

Verwandte Themen