0

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");
            }
        });
  
1
  • You need to create a class with property List<KitapAramaSonucModel> barkodliste { get; set;} and use that class as model in the action method. Commented Nov 18, 2020 at 14:34

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

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


]

Comments

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.