views:

130

answers:

1

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 about):

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 magin to pass a List to the server method. Can anyone show me what I'm doing wrong?

+1  A: 

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"}
]
}
brentkeller
I did end up writing a blog post on this for a more detailed description:http://www.brentman.com/blog/2010/01/15/asp-net-pagemethods-using-generic-lists
brentkeller