Here is my code:
public ActionResult MainMenu(int id)
{
using (WebDataContext context = new WebDataContext())
{
//var dataLoadOptions = new System.Data.Linq.DataLoadOptions();
//dataLoadOptions.LoadWith<MenuCache>(x => x.Menu);
//context.LoadOptions = dataLoadOptions;
var menu = context.MenuCaches
.AsEnumerable()
.Where(x => x.ID == id
&& (x.Local == true || x.National == true));
foreach (var item in menu)
{
if (item.Parent.Parent != null && item.Parent.ParentID == 0)
{
menu = item.Children;
}
}
return View(menu.ToList());
}
}
I found some options online on how to fix it. One was to do yield return View(menu)); but that gave me the error that ActionResult is not an iterator. The commented code in the function was another option that I found, but that didn’t work either. Any ideas? Thanks a lot.
I can’t be certain, but it’s probably something to do with the
return View(menu.ToList())as I believe the actual result(the view) isn’t executed until later in the MVC Pipe line, so theToList()isn’t executed until after yourWebDataContextis disposed. I bet this would resolve the issue: