I am using asp.net entity framework, when I tried to convert entity object list to json and return jsonresult in mvc. I used json serialize, while running the application I got this following error,
Problem with this following code:
[HttpPost]
public ActionResult CreateJson()
{
try
{
using (models context = new models())
{
var CustomerList = context.Customers.ToList();
var jsondata = new JavaScriptSerializer().Serialize(CustomerList);
string path =Server.MapPath("~/Json/");
System.IO.File.WriteAllText(path + "customer.json",jsondata);
return Json(CustomerList, JsonRequestBehavior.AllowGet);
}
}
catch (Exception ez)
{
return null;
}
}
Solution:
The database circular reference was solved by setting ProxyCreationEnabled to false. So those not to load to child objects of customer object. I have included the following line “context.Configuration.ProxyCreationEnabled = false;” in the json method.
using (models context = new models())
{
context.Configuration.ProxyCreationEnabled= false; // included the following code
var CustomerList =context.Customers.ToList();
var jsondata = new JavaScriptSerializer().Serialize(CustomerList);
string path =Server.MapPath("~/Json/");
System.IO.File.WriteAllText(path + "customer.json",jsondata);
return Json(CustomerList, JsonRequestBehavior.AllowGet);
}
Post your comments / questions
Recent Article
- How to add two numbers in Android Studio? | Source Code
- FindViewByID returns null in android studio -SOLVED
- Saving changes is not permitted in SQL SERVER - [SOLVED]
- Restore of database failed. File cannot be restored over the existing. -[SOLVED]
- One or more projects in the solution were not loaded correctly in Visual Studio 2019 | FIXED
- How to find Laptop's Battery Health?
- SOLVED-Related Field got invalid lookup: icontains error in Django
- How to enable Search Feature in Django admin?
Related Article