-1

I have this code:

var companyDetails = db.tbl_Mallstore.ToList();

dynamic result = new System.Dynamic.ExpandoObject();
result.count = 1;
result.company = companyDetails.FirstOrDefault();

JavaScriptSerializer serializer = new JavaScriptSerializer();

string output = serializer.Serialize(result);

That throws the error:

A circular reference was detected while serializing an object of type 'eDurar.Models.tbl_Mallstore

In this case it works:

dynamic result = new System.Dynamic.ExpandoObject();
result.count = count;
result.store_name = companyDetails.FirstOrDefault().store_name;

JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(result);
Taryn
  • 242,637
  • 56
  • 362
  • 405
Arun Prasad E S
  • 9,489
  • 8
  • 74
  • 87

1 Answers1

0

I had to specify manually the fields needed. I think there is some circular reference inside the other data.

int count = 0;

var companyDetails = db.tbl_Mallstore.ToList();

count = companyDetails.Count();

dynamic result = new System.Dynamic.ExpandoObject();
result.count = count;
if (count > 0)
{
    result.store = companyDetails.Select(x => new { x.store_name, x.store_id }).FirstOrDefault();
}

return this.Json(result);
Arun Prasad E S
  • 9,489
  • 8
  • 74
  • 87