0

我的控制器中有一些代码,如下所示:

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变为空。

4

1 回答 1

1

现在,您的控制器会查找用户并急切地获取收藏夹,但不会获取下一个级别。您需要稍微修改您的查询,即

var user = db.Users.Where(i => i.UserName == username)
                            .Include(i => i.UserFavorites)
                            .Include("UserFavorites.Comic");

请注意,其他查询确实会获取信息,但它们位于不同的对象图上......

于 2013-08-25T20:50:44.487 回答