将 JSON 传递给需要 List的 PageMethod范围

发布于 2024-08-17 22:41:52 字数 742 浏览 8 评论 0原文

我有一个具有以下签名的 ASP.NET PageMethod:

<WebMethod()> _
Public Shared Function SaveCodes(ByVal codes As List(Of Code)) As String

我试图从客户端将 JSON 对象传递给 PageMethod,但出现错误,指出 String 类型无法转换为 List类型;

这是我在客户端构建并发送到方法的 json:

{
“代码”:{
{ "companyID": "00000000-0000-0000-0000-000000000000", "customerType":"1", "code":"11 " },
{ "companyID": "00000000-0000-0000-0000-000000000000", "customerType":"1", "code":"21 " }
}

是我的 PageMethod 调用,(objects 是上面的 json 字符串):

PageMethods.SaveCodes(objects, successFn, errorFn);

我已经能够传递简单的数据类型和 Code 类的单个实例,但我似乎找不到将 List 传递给的神奇之处服务器方法。谁能告诉我我做错了什么?

I have an ASP.NET PageMethod with the following signature:

<WebMethod()> _
Public Shared Function SaveCodes(ByVal codes As List(Of Code)) As String

I am trying to pass a JSON object to the PageMethod from the client side, but I get an error that the type String cannot be converted to type of List<Code>.

Here is the json that I'm building on the client and sending to the method:

{
'codes' : {
{ "companyID": "00000000-0000-0000-0000-000000000000", "customerType":"1", "code":"11 " },
{ "companyID": "00000000-0000-0000-0000-000000000000", "customerType":"1", "code":"21 " }
}
}

Here is my PageMethod call, (objects is the json string above):

PageMethods.SaveCodes(objects, successFn, errorFn);

I have been able to pass in simple data types and a single instance of the Code class, but I can't seem to find the magic to pass a List to the server method. Can anyone show me what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白昼 2024-08-24 22:41:52

我能够弄清楚。下面列出了正确的表示法。缺少的主要部分是每个对象的 __type 属性。我必须在从 PageMethod 返回的列表中进行挖掘才能找到它。

{"codes":
[  
{"CompanyID":"00000000-0000-0000-0000-000000000000","Code":"11    ","CustomerType":"1","__type":"Code"},
{"CompanyID":"00000000-0000-0000-0000-000000000000","Code":"21    ","CustomerType":"1","__type":"Code"}
]
}

I was able to figure it out. The correct notation is listed below. The main piece that was missing was the __type property for each object. I had to dig around in a List that was returned from a PageMethod to find that.

{"codes":
[  
{"CompanyID":"00000000-0000-0000-0000-000000000000","Code":"11    ","CustomerType":"1","__type":"Code"},
{"CompanyID":"00000000-0000-0000-0000-000000000000","Code":"21    ","CustomerType":"1","__type":"Code"}
]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文