【发布时间】:2012-09-01 16:39:37
【问题描述】:
我正在从 javascript 向 MVC 控制器进行 ajax 调用,将对象数组传递给控制器操作。
JS代码:
function Constructor(p1, p2) {
this.foo = p1;
this.bar = p2;
}
var Obejct_Array = new Array();
Obejct_Array[Obejct_Array.length] = new Constructor("A", "B");
Obejct_Array[Obejct_Array.length] = new Constructor("C", "D");
$.post("/_Controller/_Action", { ObjectArray : Obejct_Array });
C# 代码
public Class Example
{
public string foo { get; set; }
public string bar { get; set; }
public string Prop3 { get; set; }
}
//Action in Controller
public void _Action(Example[] ObejctArray)
{
//Here the size of ObjectArray is 2 but the properties are all null. Whats the problem ?
}
javascript 数组中的两个条目都传递给控制器的操作方法,但属性值显示为 null。谁能告诉我这个问题?
【问题讨论】:
-
字符串化,然后在服务器端转换回来!
-
@adeneo 你能给个示例代码吗
-
不确定你是如何在 C# 中做到这一点的,但 Lee Taylor 已经在下面发布了一个链接。在javascript中你会做
JSON.stringify(Obejct_Array),我猜你会想要它作为一个字符串,因为它是一个数组,只是将它转换回服务器上。
标签: c# javascript jquery asp.net-mvc-3