Javascript 序列化类型为';的对象时检测到循环引用;System.Data.Entity.DynamicProxies';
我有一个Asp.NETMVC项目,数据库优先。 我在位置控制器中有一个创建操作。在action方法中,我得到的数据如下:Javascript 序列化类型为';的对象时检测到循环引用;System.Data.Entity.DynamicProxies';,javascript,json,asp.net-mvc,entity-framework,Javascript,Json,Asp.net Mvc,Entity Framework,我有一个Asp.NETMVC项目,数据库优先。 我在位置控制器中有一个创建操作。在action方法中,我得到的数据如下: // GET: /Place/Create [Authorize] public ActionResult Create() { string userId = User.Identity.GetUserId(); var places = db.Places.Where(p => p.UserId == userId); var places
// GET: /Place/Create
[Authorize]
public ActionResult Create()
{
string userId = User.Identity.GetUserId();
var places = db.Places.Where(p => p.UserId == userId);
var placesVM = new PlacesVM(places);
ViewBag.UserId = new SelectList(db.AspNetUsers, "Id", "UserName");
return View(placesVM);
}
public class PlacesVM
{
public IQueryable<Place> Places { get; set; }
public Place Place { get; set; }
public PlacesVM(IQueryable<Place> places)
{
Places = places;
Place = new Place();
}
}
我的位置视图模型如下所示:
// GET: /Place/Create
[Authorize]
public ActionResult Create()
{
string userId = User.Identity.GetUserId();
var places = db.Places.Where(p => p.UserId == userId);
var placesVM = new PlacesVM(places);
ViewBag.UserId = new SelectList(db.AspNetUsers, "Id", "UserName");
return View(placesVM);
}
public class PlacesVM
{
public IQueryable<Place> Places { get; set; }
public Place Place { get; set; }
public PlacesVM(IQueryable<Place> places)
{
Places = places;
Place = new Place();
}
}
AspNetUser:
public partial class AspNetUser
{
public AspNetUser()
{
this.AspNetUserClaims = new HashSet<AspNetUserClaim>();
this.AspNetUserLogins = new HashSet<AspNetUserLogin>();
this.Places = new HashSet<Place>();
this.UserComments = new HashSet<UserComment>();
this.AspNetRoles = new HashSet<AspNetRole>();
}
public string Id { get; set; }
public string UserName { get; set; }
public string PasswordHash { get; set; }
public string SecurityStamp { get; set; }
public string Discriminator { get; set; }
public virtual ICollection<AspNetUserClaim> AspNetUserClaims { get; set; }
public virtual ICollection<AspNetUserLogin> AspNetUserLogins { get; set; }
public virtual ICollection<Place> Places { get; set; }
public virtual ICollection<UserComment> UserComments { get; set; }
public virtual ICollection<AspNetRole> AspNetRoles { get; set; }
}
公共部分类AspNetUser
{
公共AspNetUser()
{
this.AspNetUserClaims=新哈希集
您PlacesVM
视图模型包含一个类型为Place
的属性,该属性又包含一个类型为AspNetUser
的属性,该属性又包含一个类型为集合的属性
当Json.Encode()
方法序列化模型,它序列化Place
,然后序列化AspNetUser
,然后序列化抛出错误的每个Place
,因为如果允许继续,它将序列化每个AspNetUser
,依此类推,直到系统内存耗尽
将视图模型更改为仅包含视图中所需的属性-请参阅。请注意,视图模型通常不应包含属于数据模型的属性,尤其是在视图中编辑数据时。相反,请将属性从Place
复制到需要编辑的PlaceVM
,除了AspNetUser
,如果您需要显示AspNetUser
的一些属性,那么只需包含其他属性,例如公共字符串用户名{get;set;}
旁注:当前视图模型不包含默认值(无参数)构造函数,因此当您提交时,DefaultModelBinder
将抛出异常。您的PlacesVM
视图模型包含一个类型为Place
的属性,该属性又包含一个类型为AspNetUser
的属性,该属性又包含一个类型为Collection
的属性
当Json.Encode()
方法序列化模型,它序列化Place
,然后序列化AspNetUser
,然后序列化抛出错误的每个Place
,因为如果允许继续,它将序列化每个AspNetUser
,依此类推,直到系统内存耗尽
将视图模型更改为仅包含视图中所需的属性-请参阅。请注意,视图模型通常不应包含属于数据模型的属性,尤其是在视图中编辑数据时。相反,请将属性从Place
复制到需要编辑的PlaceVM
,除了AspNetUser
,如果您需要显示AspNetUser
的一些属性,那么只需包含其他属性,例如公共字符串用户名{get;set;}
旁注:您当前的视图模型不包含默认(无参数)构造函数,因此提交时,DefaultModelBinder
将引发异常。不相关,但它必须是var model=@Html.Raw(Json.Encode(model));
(不带引号)如果要将模型序列化为javascript对象,则需要显示Place
的模型(它将包含一个属性,该属性是一个包含属性Place
的对象,导致循环引用)类AspNetUser
是否包含Place
的属性?AspNetUser包含:public virtual ICollection Places{get;set;}这是问题的原因(序列化Place
还会序列化AspNetUser
,然后序列化其属性Places
,然后必须序列化每个Place
,然后序列化AspNetUser
等等-循环引用)。您需要使用视图模型(无论如何都应该是这样)仅使用视图中所需的属性(例如)classplacevm
,并将所需属性从Place
复制到视图中,除了AspNetUser
。如果需要视图中与AspNetUser
相关的任何内容,只需为其添加一些属性(例如)int-UserID
和string
UserName`不相关,但如果要将模型序列化为javascript对象,则必须是var-model=@Html.Raw(Json.Encode(model));
(不带引号)。需要显示Place的模型(它将包含一个属性,该属性是一个包含属性Place
,导致循环引用的对象)类AspNetUser
是否包含Place
的属性?AspNetUser包含:public virtual ICollection Places{get;set;}这是问题的原因(序列化Place
还会序列化AspNetUser
,然后序列化其属性Places
,然后必须序列化每个Place
,然后序列化AspNetUser
等等-循环引用)。您需要使用视图模型(无论如何都应该是这样)只需在视图中使用所需的属性(例如)classplacevm
,并将所需的属性从Place
复制到视图中,除了AspNetUser
。如果在视图中需要与AspNetUser
相关的任何内容,只需为其添加一些属性(例如)intuserid
和string
用户名`