1

I have the api method that returns a Products list: getAllProduct() returns filled list including:

List<Product> dependProduct

but the client receives an empty dependProduct.

public class Product
{
    public string Title { get; set; }
    public int Cost { get; set; }
    public List<Product> dependProduct = new List<Product>();

}

Controller:

[Route("~/Shop/Product")]
[ResponseType(typeof(IEnumerable<Product>))]
public HttpResponseMessage Get()
{
        var data = getAllProduct(); //  has dependProduct 
        return this.Request.CreateResponse(HttpStatusCode.OK, data);
}

private List<Product> getAllProduct()
{
  return context.Products.ToList();
}

Client:

var request = new RestRequest("/Shop/Product", Method.GET);
var response = client.Execute<List<Product>>(request);
return response.Data;  // has not dependProduct  why?

1 Answer 1

5

I think the problem is that dependProduct declared as field rather than a property. Try change Product to

public class Product
{
   public Product()
   {
      dependProduct = new List<Product>();
   }

    public string Title { get; set; }
    public int Cost { get; set; }
    public List<Product> dependProduct { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, you saved me a few hours

Your Answer

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