Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Complex Javascript Array To ASP.NET Controller Method

I have asp.net mvc6 controller method which i wanted to send my complex javascript array data. I use two method to take complex array with json. First i tried the method like as below:

 public IActionResult TakeComplexArray(IList<ComplexArrayInfoModel> data) 
 {
     return PartialView(data);
 } 

Second method which i try.

 public IActionResult TakeComplexArray(ComplexArrayInfoModel[] data) 
 {
     return PartialView(data);
 }

I want to send complex javascript array like as below:

[Object, Object, Object, Object, Object]

Each object's type is my model class type ComplexArrayInfoModel. Each object has different records of this model class. More detail about this complex array is like as below:

[Object, Object, Object, Object, Object]
0: Object
1: Object
2: Object
3: Object
4: Object
length: 5
__proto__: Array[0]


Complex value has data like as below:


0: Object
Name: "aa"
Surname: "bb"
Country: null
City: 5
Age: 20
Gender: null
__proto__: Object
1: Object
Name: "ddd"
Surname: "fff"
Country: null
City: 5
Age: 20
Gender: null
__proto__: Object
2: Object
Name: "zzz"
Surname: "ggg"
Country: null
City: 5
Age: 20
Gender: null
__proto__: Object
3: Object
Name: "www"
Surname: "ccc"
Country: null
City: 5
Age: 20
Gender: null
__proto__: Object
4: Object
Name: "ccc"
Surname: "ddd"
Country: null
City: 5
Age: 20
Gender: null
__proto__: Object
length: 5
__proto__: Array[0]

I want to send this complex data to controller action with javascript function like as below:

function SendComplexData(data, row) {
   return $.ajax({
        url: '@Url.Action("TakeComplexArray")',
        /*data.complexArray is showed above schema*/
        data: JSON.stringify({ data: data.complexArray }),
        type: 'POST',
        dataType: 'html',
    });
}

I can't send this complex javascript array. How can i send this complex array to this controller action? And at the same time i couldn't send the data when i didn't use the json.stringify method.

like image 946
Jackson Casper Avatar asked Apr 19 '26 01:04

Jackson Casper


1 Answers

Did you try to use Microsoft's MVC.stringify() method to send appropriate data to mvc controller class. Your javascript function should like as below:

function SendComplexData(data, row) {
   return $.ajax({
        url: '@Url.Action("TakeComplexArray")',
        /*data.complexArray is showed above schema*/
        data: MVC.stringify({ data: data.complexArray }),
        type: 'POST',
        dataType: 'html',
    });
}

And your controller class should be like as below:

public IActionResult TakeComplexArray(IList<ComplexArrayInfoModel> data) 
{
   return PartialView(data);
} 

You can try this ajax code for send complex array to mvc6 controller action method.

like image 68
Mert Özoğul Avatar answered Apr 21 '26 15:04

Mert Özoğul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!