Click here to Skip to main content
16,020,080 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to return json data from my controller but the error i get is "a circular reference was detected while serializing an object of type".

So far i can only return data from only one table called "Projects" but when i try to include my look up table "ProjectTypes" i get the circular reference. if there is a way to return data of Projects and its correct Project Type please kindly assist me.

What I have tried:

This is what i have tried but .Include does work with return json.

public JsonResult getProjects()
{
    bool proxyCreation = dc.Configuration.ProxyCreationEnabled;
    try
    {
        //set ProxyCreation to false
        dc.Configuration.ProxyCreationEnabled = false;

        var projects = dc.Projects.Include(p => p.ProjectType);

        return Json(projects.ToList(), "application/json", System.Text.Encoding.UTF8, JsonRequestBehavior.AllowGet);

    }

    catch (Exception ex)
    {
        Response.StatusCode = (int)HttpStatusCode.BadRequest;
        return Json(ex.Message);
    }

    finally
    {
        //restore ProxyCreation to its original state
        dc.Configuration.ProxyCreationEnabled = proxyCreation;
    }
}


Below are my two classes

public partial class Project
    {
        public int ProjectID { get; set; }
        public string Description { get; set; }
        public System.DateTime StartDate { get; set; }
        public Nullable<System.DateTime> EndDate { get; set; }
        public System.DateTime ProjectedEndDate { get; set; }
        public int ProjectTypeID { get; set; }

        public virtual ProjectType ProjectType { get; set; }
    }

 public partial class ProjectType
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public ProjectType()
        {
            this.Projects = new HashSet<Project>();
        }

        public int ProjectTypeID { get; set; }
        public string Description { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Project> Projects { get; set; }
    }
Posted
Updated 23-Sep-19 11:03am
v2
Comments
Richard MacCutchan 22-Sep-19 3:20am    
The error message is telling you that objecta contains a reference to objectb, which contains a reference back to objecta. So trying to serialise that would result in an infinite loop.

1 solution

I think the problem is in your class ProjectType which has the line
this.Projects = new HashSet<Project>();
Take a look at Flattening Collection Types in this article: Working with JSON in C# & VB[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900