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:
我的javascript代码如下:
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.
但没有的数据。我尝试过的每一种可能的组合,但是在服务器端myObjArr保持为空,我尝试过很多的walkthroghts,我替换了[]列表,ICollection等等,有什么建议吗?谢谢。
1 个解决方案
#1
2
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:
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);
}
});
#1
2
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:
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);
}
});