0
@foreach (Thing thing in Model) {
    @Html.Action("someAction", "someOtherController", thing)
    //kind of a PartialView but coming from another controller
}

-

public class someOtherController: Controller
{

    public PartialViewResult someAction(Thing Model)
    {
        ...
    }

当这个 Html.Action 被调用时,我得到The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

这是我通常能够.Include()在代码中的适当位置使用来修复的错误。

但在这种情况下:

  • 在调试时,它看起来不像“东西”或包含的子实体已被处置。
  • 我可以设置断点,@Html.Action("someAction", "someOtherController", thing)但如果我在方法的第一行设置断点someAction(...),则永远无法到达。
  • 我有另一个页面使用这种局部视图,它工作得很好
  • 如果我生成内容@Html.Action("someAction", "someOtherController", thing)而不是通过局部视图调用它,它会完美运行。-

所以该调用和控制器之间一定有问题,但我无法指出是什么。知道如何进一步调试和/或解决问题吗?


有人问:

数据访问层:

public static List<Thing> GetAllThings(string[] Include = null)
{
    using (Entities ctx = new Entities())
    {
        if ((Include != null) && (Include.Length > 0))
        {
            System.Data.Entity.DbSet<Thing> things= ctx.Things;
            System.Data.Entity.Infrastructure.DbQuery<Thing> thingQuery= things.Include(Include[0]);
            for (int i = 1, c = Include.Length; i < c; i++)
                thingQuery= thingQuery.Include(Include[i]);
            return thingQuery.ToList();
        }
        return ctx.Things.ToList();
    }
}

在控制器中:

public ActionResult Index()
{
    string[] include = new string[] { "Stuff1.Stuff2", "Stuff4.Stuff5.Stuff6", "Stuff7.Stuff8" };
    List<Things> things = ThingsManager.GetAllThings(include).OrderBy(x => x.Name).ToList();
    return this.View(things);
}
4

1 回答 1

2

异常消息说明了一切:在尝试枚举延迟加载的相关实体之前,您的数据库上下文超出了范围。

检查以确保您正在急切加载此代码块操作的所有相关实体:

@foreach (item dto in items) {
    @Html.Action("someAction", "someOtherController", item) //kind of PartialView but coming from another controller
于 2014-01-30T16:23:46.763 回答