向父母显示列表数据子

时间:2018-01-06 05:23:36

标签: asp.net-mvc

我正在尝试获取包含主类别ID和名称信息的子类别列表。请注意:子类别是主类别的子类。所以这就像一个孩子称其相关的父母。我已经尝试过代码,但问题是我得到了错误 -

  

System.ObjectDisposedException:'ObjectContext实例已经   处置并且不能再用于需要的操作   连接“。

我已将图片附加到您可以看到错误和数据调试的位置,以便您更好地了解来自数据库的数据结构。如果您有任何疑惑,请询问问题。

debug

error

控制器:

public ActionResult ProductCreate()
        {
            using (MyappWebDbEntities db = new MyappWebDbEntities())
            {
                ViewBag.SubCategories = db.SubCategories.ToList();
                return View();
            }
        }

查看:

<select class="form-control" id="categoryId">
    @foreach (var item in ViewBag.SubCategories)
    {
        <option value="@item.SubCategoryId">@item.SubCategoryName by-> @item.Category.CategoryName</option>
    }
    </select><br />

1 个答案:

答案 0 :(得分:0)

您应尝试使用以下代码替换子类别提取代码:

db.SubCategories.Include(x => x.Category).ToList()