My project is C# .NET, MVC 5, EF6. I'm getting an ObjectDisposedException
on using an object in a view that has been gotten from the database. I've read probably EVERY similar question, but .Include()
does NOT work; I'm thinking the problem doesn't have anything to do with lazy loading.
The controller method:
public ActionResult Browse()
{
List<QuestionGroup> questionGroupsWithLinks = new List<QuestionGroup>();
using (CLASSContext context = new CLASSContext())
{
questionGroupsWithLinks = context.QuestionGroup.Include(qg => qg.Questions.Select(q => q.Answers))
.Where(qg => qg.QuestionGroupID == 128).ToList();
return View("Browse", questionGroupsWithLinks);
}
}
I've tried having the using statement not wrap around the view, I've tried declaring questionGroupWithLinks
in different places, I've tried iterating through questionGroupWithLinks
and assigning one of its properties in hopes that that would load it (didn't make any difference, because the problem is only in the view. It's always loaded as long as you're in the controller method), and I've tried other things as well.
The view (simplified):
@model List<CLASSOnlineAssessments.Models.Assessments.QuestionGroup>
<div class="page-copy">
@if (Model != null)
{
foreach (QuestionGroup qg in Model)
{
//More code here; but it always fails before this point.
}
}
</div>
I've tried using Model.First()
to access a question group instead of foreach
, but that doesn't make any difference.
Let me know if I can clarify anything or post more information.