Handling Circular References ASP.NET MVC 4 Json Serialization - asp.net mvc 4
In this post I will explain Circular Reference Serialization Error in MVC Entity frame work. In my latest project I need to get data from database for purpose of Autocomplete.So I need data as JSON format.
When I request the “http://localhost:xxxx/Student/GetStudentJson”
My http GetStudentJson Method is bellow
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
public class StudentController : Controller
{
private SchoolDBContext db = new SchoolDBContext();
public JsonResult GetStudentJson()
{
var students = db.Students .Include(s => s.Standard).Include(s => s.StudentAddress);
return Json(students, JsonRequestBehavior.AllowGet);
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
My mode class are following
Student.cs
using System;
using System.Collections.Generic;
namespace MvcApplication1.Models
{
public class Student
{
public Student()
{
this.Courses = new List<Course>();
}
public int StudentID { get; set; }
public string StudentName { get; set; }
public int StandardId { get; set; }
public virtual Standard Standard { get; set; }
public virtual StudentAddress StudentAddress { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
}
Standard.cs
using System;
using System.Collections.Generic;
namespace MvcApplication1.Models
{
public class Standard
{
public Standard()
{
this.Students = new List<Student>();
}
public int StandardId { get; set; }
public string StandardName { get; set; }
public string Description { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
StudentAddress.cs
using System;
using System.Collections.Generic;
namespace MvcApplication1.Models
{
public class StudentAddress
{
public int StudentID { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public virtual Student Student { get; set; }
}
}
Now I will explain How to solve this problem
In this error occur because the student entity class includes Standard and StudentAddress class entity.
But In GetStudentJson Method tries to convert only Student class object to Json.So that I got the above error.
To solve this error we rewrite the GetStudentJson method
Solution One
public JsonResult GetStudentJson()
{
db.Configuration.ProxyCreationEnabled = false;
var students = db.Students;
return Json(students, JsonRequestBehavior.AllowGet);
}
The Out Put is Look like bellow
[{"StudentID":1,"StudentName":"John","StandardId":1,"Standard":null,
"StudentAddress":null,"Courses":[]}]
Solution Two
In this type we get selected error
public JsonResult GetStudentJson()
{
var students = from s in db.Students select new { s.StudentID, s.StudentName,s.StandardId };
return Json(students, JsonRequestBehavior.AllowGet);
}
Please share this post with your friends. If it's useful to you. Thanks!.