0

Here is my js code on the page:

var data = [
  ["Benz", "Kia", "Nissan", "Toyota", "Honda"],
  ["2008", 10, 11, 12, 13],
  ["2009", 20, 11, 14, 13],
  ["2010", 30, 15, 12, 13]
];

$.ajax({
     url: "/Home/SaveData",
     type: "POST",
     contentType: 'application/json',
     data:JSON.stringify({ data: data }),,
     dataType: 'json',
});

Here is my mvc action given below:

[HttpPost]
public string save(string[][] data)

Null value was posted, However, I tried to use List< string> instead of string[][] , the hold string of array collected as an item of the list (not a row to a list item).

How to bind this 2D array at server side?

Systematix Infotech
  • 2,345
  • 1
  • 14
  • 31

2 Answers2

0

In your ajax don't do stringify, Just try putting as data itself,

$.ajax({
 url: "/Home/SaveData",
 type: "POST",
 contentType: 'application/json',
 data: { data: data },
 dataType: 'json',
});

If you want to stringify it, You need to override the default model binder refer this CustomModelBinder for overriding the model binder.

Community
  • 1
  • 1
kriznaraj
  • 484
  • 2
  • 11
0

Change your code like below

var data = [
 ["Benz", "Kia", "Nissan", "Toyota", "Honda"],
 ["2008", 10, 11, 12, 13],
 ["2009", 20, 11, 14, 13],
 ["2010", 30, 15, 12, 13]
 ];

$.ajax({
 url: "/Home/SaveData",
 type: "POST",
 contentType: 'application/json',
 data:{ data: data },
 dataType: 'json',
});

Now the data can be binded at the server side.

Madhu
  • 2,416
  • 3
  • 15
  • 33