c# - MVC 3 sending class JSON -


my parameter kontrolkasamtekstlista null data controller actionresult kontrolkasubmit() send action ajax looks okay.

class

using system.collections.generic;  namespace modul_stypendia.kontrolki {     public class kontrolkasamtekst     {         public int id { get; set; }         public int kolejnosc { get; set; }         public string tekst { get; set; }         public int wypelnia { get; set; }         public list<string> przyklad { get; set; }      } } 

here list empty important

[httppost]         public actionresult kontrolkasubmit(list<kontrolkasamtekst> kontrolkasamtekstlista)         {             system.threading.thread.sleep(2000);                 return json(new { msg = "successfully added " + kontrolkasamtekstlista[0].tekst });         } 

ajax

$("#waznytest").click(function () {          var kontrolkasamtekst = {             id: iddane,             kolejnosc: kolejnoscdane,             tekst: tekstdane,             wypelnia: wypelniadane,             przyklad: przykladdane         }          kontrolkasamtekstlista = [];         kontrolkasamtekstlista.push(kontrolkasamtekst);         kontrolkasamtekstlista.push(kontrolkasamtekst);         kontrolkasamtekstlista.push(kontrolkasamtekst);          $.ajax({         url: '@url.action("kontrolkasubmit", "stystypendia")',         datatype: 'json',         data: {             kontrolkasamtekstlista: kontrolkasamtekstlista         },         type: 'post',         traditional: true,         async: false,         success: function (data) {             $('#target').html(data.msg);         }     });      }); 

sent data looks okay

enter image description here

sent data

enter image description here

correct answer

 $.ajax({             url: '/stystypendia/kontrolkasubmit',             type: 'post',             async: false,             contenttype: "application/json; charset=utf-8",              data: json.stringify(kontrolkasamtekstlista)         }); 

the best example link

you need stringify data, , not pass in object, array directly:

$.ajax({         url: '@url.action("kontrolkasubmit", "stystypendia")',         contenttype: "application/json; charset=utf-8",          data: json.stringify(kontrolkasamtekstlista),         type: 'post',         async: false,         success: function (data) {             $('#target').html(data.msg);         } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -