情况是这样的:我想遍历一个带有输入控件的表,收集值然后将它们提交给 ASP.NET PageMethod
将数据保存到数据库中。我已经弄清楚了所有的集合,但收到一个错误消息,指出该字符串无法转换为 Dictionary<string, object>
.
所以我最终将类似这样的东西传递给具有以下签名的方法
[
{ 'id': '383840923', 'name': 'fred', 'car':'honda' },
{ 'id': '243', 'name': 'joe', 'car':'honda' },
{ 'id': '4323423', 'name': 'paul', 'car':'honda' },
{ 'id': '38384234230923', 'name': 'ted', 'car':'honda' },
]
public static bool SaveData(Dictionary<string, object>[] items) {...}
我知道如果声明正确,我可以来回传递整个类对象,ASP.NET 会为我处理转换,但我不需要传递整个类,只需要传递几个属性。
编辑:我正在使用 jQuery 将帖子发回服务器。
我在这里做错了什么?
最佳答案
ASP.NET AJAX will automatically deserialize that for you if you use a DTO .服务器端的类似内容将匹配您发送的 JSON 数组:
public class PeopleAndCarsDTO
{
public int id { get; set; }
public string name { get; set; }
public string car { get; set; }
}
public static bool SaveData(List<PeopleAndCarsDTO> items) {...}
关于asp.net - 将 JavaScript 对象数组传递给 PageMethod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/871709/