I am trying to follow the instructions provided in this video on Youtube.
Here is the code 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);
}
This is how it looks 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
});
});
The database table "Drinks_category" has 3 columns: Id_category, Name_category, and Parent.
However, I encountered an error:
A circular reference was detected while serializing an object of type 'WebApplication3.Models.Framework.Drinks_Category'.
The Drinks_Category class is defined as follows:
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; }
}
I would appreciate any help with resolving this issue. Thank you!