Stack Exchange
A circular reference was detected while serializing an object of type JSON MVC
I follow the instructions of this video Youtube
In my controller:
public ActionResult loaddatacate()
        {
            BBDbModel context = new BBDbModel();
            context.Configuration.ProxyCreationEnabled = false;
            var data = context.Drinks_Category.ToList();
            return Json(new { data = data }, JsonRequestBehavior.AllowGet);
        }

In my view: 
<script type="text/javascript">
        $(document).ready(function (e) {
            $("#example1").DataTable({
                "ajax": {
                    "url": "/Admin/AdminHome/loaddatacate",
                    "type": "GET",
                    "datatype": "json"
                },
                "columns": [
                    { "data": "Id_category", "autowidth": true },
                    { "data": "Name_category", "autowidth": true },
                    { "data": "Parent", "autowidth": true }
                ]
            });
            var Parent =  @Html.Raw(Json.Encode(ViewBag.Parent));
            $(".parent").autocomplete({
                source: Parent
            });
        });

Table "Drinks_category" in database have 3 columns: Id_category,Name_category,Parent
Error is:
A circular reference was detected while serializing an object of type 'WebApplication3.Models.Framework.Drinks_Category'.
Drinks_Category class:
public partial class Drinks_Category
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Drinks_Category()
        {
            Drinks = new HashSet<Drink>();
            Drinks_Category1 = new HashSet<Drinks_Category>();
        }

        [Key]
        [StringLength(10)]
        public string Id_category { get; set; }

        [StringLength(20)]
        public string Name_category { get; set; }

        [Required]
        [StringLength(10)]
        public string Parent { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Drink> Drinks { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Drinks_Category> Drinks_Category1 { get; set; }

        public virtual Drinks_Category Drinks_Category2 { get; set; }
    }

Please help me. Thanks!

how is the db laid out, and what are the restrictions?

I do not understand your question, BBDbModel is a DbContext

Var data is an exact list, I thought it was an error due to JSON

The circular reference is coming from a table. We don't know how it looks like neither the data mappings

Please take a look at this photo link

Maybe post your Drinks_Category class too please.

Just update the post with code and images. Outside links are discouraged

Can you put a breakpoint on data before your return call to see if your results are good i.e. non proxy classes with the number of results you expect to have and they are only the Drinks_Category objects and no other eager loaded entities?

An easier alternative to solve this problem is to return an string, and format that string to json with JavaScriptSerializer.
public string GetEntityInJson()
{
   JavaScriptSerializer j = new JavaScriptSerializer();
   var entityList = dataContext.Entitites.Select(x => new { ID = x.ID, AnotherAttribute = x.AnotherAttribute });
   return j.Serialize(entityList );
}

It is important the "Select" part, which choose the properties you want in your view.
This helps render a view with less data, just with the attributes you need, and makes your web run faster.

As far as I know this kind of error can be risen when you try to serialize for example a class say class1 that reference by a property another class say class2 and in turn class2 reference with a property class1.  The circular reference is because from class1 you go to class2 and from class2 to class1

Instruct the serializer to ignore the loop reference like:
var data = JsonConvert.SerializeObject(myList, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore});

And return it like:
return Json(new { data = data }, JsonRequestBehavior.AllowGet);


Error: Request unknow parameter 'Id_category' for row 0 column 0.  :(

I check network XHR: is data?
"[{"Id_category":"MILKTEA   ","Name_category":"Trà sữa             ","Parent":"CATE      "},{"Id_category":"MILKTEA   ","Name_category":"Trà sữa             ","Parent":"CATE      "}]"
