【发布时间】:2016-06-05 11:53:57
【问题描述】:
我有一个 Asp.NET MVC 项目,首先是数据库。 我在 Place 控制器中有一个 Create 动作。在操作方法中,我得到这样的数据:
// 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);
}
我的 Place 视图模型如下所示:
public class PlacesVM
{
public IQueryable<Place> Places { get; set; }
public Place Place { get; set; }
public PlacesVM(IQueryable<Place> places)
{
Places = places;
Place = new Place();
}
}
我的地方模型:
public partial class Place
{
public string Id { get; set; }
public string UserId { get; set; }
//TODO: Validare cordonate
public decimal X { get; set; }
public decimal Y { get; set; }
[Display(Name = "Title")]
[Required]
[StringLength(250, MinimumLength = 5)]
public string Titlu { get; set; }
[Display(Name = "Description")]
[Required]
[StringLength(500, MinimumLength = 10)]
public string Descriere { get; set; }
[Required]
[Range(0, 1)]
public byte Public { get; set; }
public virtual AspNetUser AspNetUser { get; set; }
}
AspNet 用户:
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; }
}
现在我想在页面的 javascript 部分使用 Model.Places proprietes。我该怎么做?
我尝试了以下方法:
<script>
var model = '@Html.Raw(Json.Encode(Model))';
</script>
但是我收到了这个错误:
{"A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.Place_084A987E8F6FBE574A22E813FE314F2894AF728F244BDD6582AF50929FF1161D'."}
我在 SO 上查看了以下链接,但未能解决我的问题:
【问题讨论】:
-
不相关,但如果您想将模型序列化为 javascript 对象,则需要为
var model = @Html.Raw(Json.Encode(Model));(不带引号)。 -
您需要显示
Place的模型(它将包含一个属性,该属性是一个包含属性Place的对象,导致循环引用) -
类
AspNetUser是否包含Place的属性? -
AspNetUser 包含:公共虚拟 ICollection
Places { get;放; } -
这就是问题的原因(序列化
Place也序列化AspNetUser,然后序列化其属性Places,然后必须序列化每个Place序列化AspNetUser等等和依此类推——循环引用)。您将需要使用视图模型(无论如何都应该使用),其中仅包含视图中所需的属性(例如)class PlaceVM,并将您需要的属性从Place复制到其中,AspNetUser除外。如果您需要在视图中与AspNetUser相关的任何内容,只需为其添加一些属性(例如)int UserID和stringUserName`
标签: javascript json asp.net-mvc entity-framework