I use Wcf Service
and JS
.
I send information to my service by AJAX
.
I have a function in the service that accepts the following object:
[DataContract]
public class Business
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Desc { get; set; }
[DataMember]
public List<BusinessService> Services { get; set; }
}
the BusinessService.cs:
[DataContract]
public class BusinessService
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Desc { get; set; }
[DataMember]
public int LongTime { get; set; }
}
I parse the objects in my JS to JSON
and send them to the service.
Here the json that send to the wcf function:
"{"ManageBusiness":{"Name":"aaaaaa","Desc":"aaaaaaaaaaaa","Services":[{"Name":"aaaaaa","Desc":"aaaaaa","LongTime":30},{"Name":"aaaaaa1","Desc":"aaaaaa","LongTime":30}]}}"
Everything works as it should except the Services
list.
The list comes to the wcf function as null.
I have no idea why this is happening, I would appreciate help?
You cannot instantiate a List<T>
from JSON data. What you can do, is replace :
[DataContract]
public class Business
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Desc { get; set; }
[DataMember]
public List<BusinessService> Services { get; set; }
}
with
[DataContract]
public class Business
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Desc { get; set; }
[DataMember]
public BusinessService[] Services { get; set; }
}
That is, replace List<T>
with Array of T
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With