【发布时间】:2015-11-14 15:48:42
【问题描述】:
我有一个视图模型:
public class UserCollectionView
{
public CardCollection CardCollections { get; set; }
public Card Cards { get; set; }
}
我有一个列表视图控制器:
public ActionResult ViewCollection(int? page)
{
var userid = (int)WebSecurity.CurrentUserId;
var pageNumber = page ?? 1;
int pageSize = 5;
ViewBag.OnePageOfCards = pageNumber;
if (Session["CardCollection"] != null)
{
var paging = Session["CardCollection"].ToString.();
return View(paging.ToPagedList(pageNumber, pageSize));
}
var viewModel = from c in db.Cards
join j in db.CardCollections on c.CardID equals j.CardID
where (j.NumberofCopies > 0) && (j.UserID == userid)
orderby c.Title
select new UserCollectionView { Cards = c, CardCollections = j };
Session["CardCollection"] = viewModel;
return View(viewModel.ToPagedList(pageNumber, pageSize));
我正在尝试使用 PagedList 将分页添加到结果中。当我不使用从单个视图中返回来自 2 个数据库的数据的查询时,我已经能够做到这一点。 As shown here
我的最终结果如下所示:
Cards.SeveralColumns CardCollections.ColumnA CardCollections.ColumnB
Row 1 Data from Cards Table A from CardCollections B from CardCollections
Row 2 Data from Cards Table A from CardCollections B from CardCollections
Row 3 Data from Cards Table A from CardCollections B from CardCollections
等等...我收到一个错误
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
我已经尝试过各种 SQL 语句,但无法使其适合我的视图模型。在 SQL Management Studio 中,这会返回正确的结果
Select * from Cards Inner Join CardCollections On Cards.CardID = CardCollections.CardID where CardCollections.UserID = 1 and CardCollections.NumberofCopies > 0;
我需要一种在会话中传递查询的方法,以便分页正常运行。任何建议表示赞赏。 :)
【问题讨论】:
-
这个错误是在控制器中还是在渲染视图时出现?
-
在最初呈现视图后选择第二页时会发生这种情况。错误发生在控制器中。它在 CardCollection 会话中传递了很多东西,但结果视图状态为
The operation cannot be completed because the DbContext has been disposed.
标签: mysql asp.net-mvc asp.net-mvc-4 session pagination