我遵循這個視頻Youtube而串行化類型JSON的目的MVC
的指令。在我的控制器中檢測到循環引用:
public ActionResult loaddatacate()
{
BBDbModel context = new BBDbModel();
context.Configuration.ProxyCreationEnabled = false;
var data = context.Drinks_Category.ToList();
return Json(new { data = data }, JsonRequestBehavior.AllowGet);
}
筆者認爲:
<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
});
});
數據庫中的表「Drinks_category」有3列:Id_category,Name_category,Parent
錯誤是:
在序列化「WebApplication3.Models.Framework.Drinks_Category」類型的對象時檢測到循環引用。
Drinks_Category類:
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; }
}
請幫助我。謝謝!
如何數據庫的佈局,有哪些限制? – efekctive
我不明白你的問題,BBDbModel是一個DbContext –
Var數據是一個確切的列表,我認爲這是一個錯誤,由於JSON –