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

3
  • Please add the error that you are getting Commented Apr 14, 2016 at 7:12
  • There is no any errors. I only receive Result = true, and 3 categories, with names, but with out products Commented Apr 14, 2016 at 7:14
  • Of course I have more properties in Category and Product. I show only some of them for example. But all of them are simple (int, string) Commented Apr 14, 2016 at 7:16

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.