asp.net - 将 JavaScript 对象数组传递给 PageMethod

标签 asp.net javascript pagemethods

情况是这样的:我想遍历一个带有输入控件的表,收集值然后将它们提交给 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/

相关文章:

.net - ASP.Net 配置页在哪里?

c# - 代码格式中的缩进和对齐

javascript - 如何以编程方式清除/重置 React-Select?

javascript - 在多个 settimeout 上使用 Async/Await

asp.net - 如何让 Ajax.Net PageMethod 返回 JSON

asp.net - 如何从 Asp.Net MVC 中的 Context.Request 获取 BaseUrl?

c# - 如何更新 OWIN CookieAuthenticationProvider 中的声明?

javascript - 在 Node js 中处理嵌套 promise 的错误

asp.net - 使用 jquery 调用 ascx 页面方法