skip to Main Content

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 ?

2

Answers


  1. 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>
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search