简体   繁体   中英

Unable to get array in controller from ajax

Not getting array of data from ajax to controller.

$.ajax({
      type: "POST",
      url: "/Home/List",
      traditional: true,
      contentType: 'application/json',
      data: {
        "Query": JSON.stringify(Query) //change this
        success: function() {}
      });,

And array of Query :

 0: {label: "abc:", value: "123", type: "Select"} 1: {label: "xyz", value: "Hum", type: "text"}

Can anyone help ?

Try something like this

<script type="text/javascript">

 var  query=[{label: "abc:", value: "123", type: "Select"},{label: "abc:", value: "1232", type: "Select"} ];
  $.ajax({ type: "POST",
   url: "/Home/List", 
   traditional: true, 
   contentType: 'application/json',
   data: JSON.stringify(query),
   success: function (){

   } });

</script>

I think something along this may work for you.

 function sendarray() {
        var arr = [];
        var json = {
            "label": 'abc',
            "value": 1234,
            "Name": 'Name'
        };
        arr.push(json);
        json = {
            "label": 'abc2',
            "value": 1234,
            "Name": 'Name2'
        };
        arr.push(json);    
        var myarray = JSON.stringify(arr);
        $.ajax({
            url: '/controller/GetArray',
            type: 'POST',            
            data: { array: myarray },
            success: function (data) {
                    //Do something
            },
            error: function () {
                //Do something
            }
        });
    }

then in the controller

public JsonResult GetArray(string array)
        {
            var obj = JsonConvert.DeserializeObject<object>(array);
            return Json("");
        }

This will send an string with all the data in the array to the controller, then you turn string with json format into an object list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM