我的控制器中有一些代码,如下所示:
var db = new ComicEntities();
var user = db.Users.Where(i => i.UserName == username).Include(i => i.UserFavorites);
var AuthorComics = db.ComicAuthors.Where(i => i.User_Id == 1)
.Include(i => i.Comic)
.Include("Comic.Chapters")
.Include("Comic.Chapters.Pages");
var Comics = AuthorComics.Select(i => i.Comic).Include("Chapters").Include("Chapters.Pages");
return View(new LocalUserViewModel { User = user.First(), Comics = Comics.ToList() });
我的观点是这样的:
@model Comics.Models.LocalUserViewModel
@foreach(var favorite in @Model.User.UserFavorites)
{
<div class="chapter-info">
<span class="chapter-info-title misc-comic-title">@favorite.Comic.Title</span>
<span class="chapter-info-page misc-comic-info">@favorite.DateFavorited</span>
</div>
}
问题是在foreach
循环的第一次迭代之后,该属性@favorite.Comic
变为空。