Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a list within an object to Wcf Service

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?

like image 807
Hodaya Shalom Avatar asked Oct 02 '22 19:10

Hodaya Shalom


1 Answers

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

like image 57
George Mavritsakis Avatar answered Oct 07 '22 03:10

George Mavritsakis