Trinity
Trinity

Reputation: 484

How to pass array of json object to controller?

I tried many ways to do it but everytime ajax send null to controller. I have json object array like as following style. I would like to send this array to my controller but every time controller parameter is null. What am i doing wrong ?

   {
   "barkodliste":[
      {
         "ToplamSonuc":0,
         "KitapKey":12,
         "DemirbasNo":112,
         "Isbn":"fghfgh",
         "KutuphaneSiraNo":"fghfghh/fghfg",
         "KitapAdi":"0000000000112 - Fghfg",
         "Adet":0,
         "Durum":1
      },
      {
         "ToplamSonuc":0,
         "KitapKey":15,
         "DemirbasNo":115,
         "Isbn":"sdlfkjsd",
         "KutuphaneSiraNo":"fsdkljflskdj/dsfkl",
         "KitapAdi":"0000000000115 - Sdlfkjsd",
         "Adet":0,
         "Durum":1
      }
   ]
}

KitapAramaSonucModel.cs

  public class KitapAramaSonucModel
    {
        public int ToplamSonuc { get; set; }
        public int KitapKey { get; set; }
        public int DemirbasNo { get; set; }
        public string Isbn { get; set; }
        public string KutuphaneSiraNo { get; set; }
        public string KitapAdi { get; set; }
        public string Yazar { get; set; }
        public int Adet{ get; set; }
        public int Durum { get; set; }
        public string Barkod { get; set; }
    }

Controller

[HttpPost]
public async Task<JsonResult> BarkodBasilacakKitaplariGetir(List<KitapAramaSonucModel> barkodlar)
{
   //Do something with barkodlar    

}

cshtml

var barkodliste = $("#books").data("kendoMultiSelect").dataItems();
            
        $.ajax({
            cache: false,
            url: "/Kutuphane/BarkodBasilacakKitaplariGetir",
            type: 'POST',
            contentType: "application/json charset=utf-8",
            traditional: true,
            data: JSON.stringify({ barkodlar: barkodliste }),
            success: function (data) {
                alert("success");
            }
        });
  

Upvotes: 0

Views: 1040

Answers (2)

Stanislav
Stanislav

Reputation: 470

  1. Try to call using Fiddler or Postman
  2. Try to see how the call occurs (Fiddler or Browser), maybe you're not sending an array
  3. Try add [FromBody] BarkodBasilacakKitaplariGetir([FromBody] List

Your controller is waiting for:

[
  {
     "ToplamSonuc":0,
     "KitapKey":12,
     "DemirbasNo":112,
     "Isbn":"fghfgh",
     "KutuphaneSiraNo":"fghfghh/fghfg",
     "KitapAdi":"0000000000112 - Fghfg",
     "Adet":0,
     "Durum":1
  },
  {
     "ToplamSonuc":0,
     "KitapKey":15,
     "DemirbasNo":115,
     "Isbn":"sdlfkjsd",
     "KutuphaneSiraNo":"fsdkljflskdj/dsfkl",
     "KitapAdi":"0000000000115 - Sdlfkjsd",
     "Adet":0,
     "Durum":1
  }


]

Upvotes: 0

Martin Z
Martin Z

Reputation: 228

In the ajax request, try: data: JSON.stringify(barkodliste)

Upvotes: 1

Related Questions