2

I have a typical response

ResultModel
{
    Bool Result,
    Object Response
}

I want to send for example categories with products

var response = new List<Category> 
{
    new Category {Name = "1", Products = new List<Product> {new Product {Name = "1_1"}, new Product {Name = "1_2"}} },
    new Category {Name = "2", Products = new List<Product> {new Product {Name = "2_1"}, new Product {Name = "2_2"}} },
    new Category {Name = "3", Products = new List<Product> () },
}

And my response

return new ResultModel
        {
            Response = response,
            Result = true
        };

But I can't receive Products property in response. Anybody knows why? Thanks

Badfishmaan
  • 318
  • 3
  • 10

1 Answers1

2

You are only getting the property Name because it's a primitive type and serializer knows how to serialize and deserialize it. If you want to obtain more complex objects you must be sure that serializer know how to do it.

You can achieve this implementing the interface Iserializable in your object and after you are sure that you can serialize it you can use it in your webApi.

https://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable(v=vs.110).aspx

And here you have an SO thread about this Using Serializable attribute on Model in WebAPI

Community
  • 1
  • 1
acostela
  • 2,597
  • 3
  • 33
  • 50