public class Category
{
public int id { get; set; }
public string name { get; set; }
public ICollection<Section> Sections { get; set; }
}
public class Section
{
public int id { get; set; }
public string name { get; set; }
public int CategoryId { get; set; }
public ICollection<Topic> Topics { get; set; }
}
public class Topic
{
public int id { get; set; }
public string name { get; set; }
public int SectionId { get; set; }
}
SQL Mgmt Studioでは、3つのテーブルの間に1対多の関係を作成しました。私は唯一のカテゴリテーブルからデータを取得
@model IEnumerable<Models.Category>
<h2>Table of Contents</h2>
<ul>
@foreach (var item in Model)
{
<li>@Html.DisplayFor(model => item.name)
@if (item.Sections != null)
{
<ul>
@foreach (var sec in item.Sections)
{
<li>@Html.DisplayFor(model => sec.name)
@if (sec.Topics != null)
{
<ul>
@foreach (var topic in sec.Topics)
{
<li>@Html.DisplayFor(model => topic.name)</li>
}
</ul>
}
</li>
}
</ul>
}
</li>
}
</ul>
:
public ActionResult Index()
{
return View(_db.Categories.ToList());
}
その後、私の見る:私のコントローラのActionResultは、このようになります。したがって、カテゴリデータを正しく取得して外側のリストを作成しますが、モデル内の関連するセクションおよびトピックにはデータが入力されません。私は間違って何をしていますか?
以下のコメントにDavid Clough氏の提案ごとに回答を実装しようとしました。私のコントローラでSystem.Data.Entityを参照した後、ラムダ式のバージョンはコンパイルと実行が可能でしたが、モデルにアクセスしようとするforeachループでエラーが発生しました。 InnerExceptionのエラーは次のとおりです。 "無効な列名 'Section_id'。\ r \ n無効な列名 'Category_id'。\ r \ n無効な列名 'Section_id'。"
public ActionResult Index()
{
var TOC = _db.Categories.Include(c => c.Sections.Select(s => s.Topics));
return View(TOC);
}
これは助けてください http://stackoverflow.com/questions/5905716/how-do-i-eagerly-include-the-child-and-grandchild- Entity-Ent-Entit-in-Entit –