Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net web api returns list of empty objects [closed]

I have an action, which shoeuld return a limited list of store items from a DB, here the code below

    [HttpGet]
    public IEnumerable<ProductInfo> GetTop(int amount = 5)
    {
        var mostPopulardProductIds = dbe.SalesOrderDetail.GroupBy(p => p.ProductID)
            .OrderByDescending(p => p.Count())
            .Take(amount)
            .Select(p => p.Key)
            .ToList();

        var result = dbe.Product
            .Where(p => mostPopulardProductIds.Contains(p.ProductID))
            .Select(p => new ProductInfo()
            {
                Id = p.ProductID,
                Name = p.Name
            }
            ).ToList();
        return result;
    }

and here's my ProductInfo class

[DataContract]
public class ProductInfo
{
    public int Id { get; set; }
    public string Name { get; set; }
    //public
}

But when I try to access it via browser, I get list of empty objects, which are looking like the 1st pic

But debugger shows me, that result variable containing normal objects with data

smlresult

debugger result

I've tried to return result through Request.CreateResponse method, but this still does not work

like image 906
bogdanbrizhaty Avatar asked Oct 29 '25 09:10

bogdanbrizhaty


1 Answers

Please add DataMember attribute to class properties, change ProductInfo class

[DataContract]
public class ProductInfo
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string Name { get; set; }

}

More about Data Member

like image 129
Naruto Avatar answered Oct 31 '25 13:10

Naruto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!