Reputation: 1784
I have this controller:
public HomeController : Controller
{
public ActionResult(MyObj[] myObjArr)
{
}
}
public class MyObj
{
public Datetime d {get;set;}
public int x{get;set;}
public string yy {get;set;}
}
My javascript code is the following:
var obj = { d:new Date(), x : 10, yy : 'hello' };
$.ajax({
type: "POST",
url: server,
dataType:"json",
data: {obj,obj},
success: function(data) {
alert(data);
}
});
It simply dont work, i've tried:
$.JSON({obj,obj});
JSON.stringify({obj,obj});
On data but nothing. Every possible combination i've probably tried out but on server side myObjArr just keeps at null, i've tried numerous walkthroghts, i replaced [] to List, ICollection, etc Any suggestions? Thanks.
Upvotes: 2
Views: 1039
Reputation: 1911
This seems fine for me.
public HomeController : Controller
{
public ActionResult(MyObj[] myObjArr)
{
}
}
public class MyObj
{
public Datetime d {get;set;}
public int x{get;set;}
public string yy {get;set;}
}
JS:
function makeParams(arr, namespace) {
var tempObj = {};
for (var i = 0; i < arr.length; i++) {
var o = arr[i];
for (var k in o) {
tempObj[namespace + "[" + i + "]." + k] = o[k];
}
}
return tempObj;
}
var arr = [{ d:new Date(), x : 10, yy : 'hello' }];
var namespace = "myObjArr";
var data = makeParams(arr, namespace); //eg. { 'myObjArr[0].x': 10 }
$.ajax({
type: "POST",
url: server,
dataType:"json",
data: data,
success: function(data) {
alert(data);
}
});
Upvotes: 2