为简单起见,我有 3 个实体(一对多,从左到右):
课程 -> 模块 -> 章节
一门课程可以有多个模块,一个模块可以有多个章节。
我在控制器中的所有内容是:
Course course = db.Courses.Find(id);
return View(course);
我试图使用包含但它似乎没有评估(这似乎不起作用Course course = db.Courses.Include("Modules").Find(id);
:)
所以我顺其自然。在我看来,我有这个嵌套列表:
<ul>
@foreach (var item in Model.Modules)
{
<li>@module.Title
<ul>
@foreach (var item in module.Chapters)
{
<li>@chapter.Title</li>
}
</ul>
</li>
}
</ul>
这会自动工作吗?
最后,我应用了一个 SortOrder 列,以便安排子实体。我知道这应该在查询中完成,但我该怎么做呢?
谢谢!任何信息或建议将不胜感激。
更新
课程班
public partial class Course
{
public Course()
{
this.Modules = new HashSet<Module>();
this.Assets = new HashSet<Asset>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Author { get; set; }
public System.DateTime CreateDate { get; set; }
public bool IsDeleted { get; set; }
public int IndustryId { get; set; }
public virtual ICollection<Module> Modules { get; set; }
public virtual ICollection<Asset> Assets { get; set; }
public virtual Industry Industry { get; set; }
}
模块类
namespace RocketLabs.Models
{
using System;
using System.Collections.Generic;
public partial class Module
{
public Module()
{
this.Chapters = new HashSet<Chapter>();
this.Assets = new HashSet<Asset>();
}
public int Id { get; set; }
public string Title { get; set; }
public System.DateTime CreateDate { get; set; }
public int CourseId { get; set; }
public bool IsDeleted { get; set; }
public short SortOrder { get; set; }
public virtual Course Course { get; set; }
public virtual ICollection<Chapter> Chapters { get; set; }
public virtual ICollection<Asset> Assets { get; set; }
public virtual Exam Exam { get; set; }
}
}
章类
namespace RocketLabs.Models
{
using System;
using System.Collections.Generic;
public partial class Chapter
{
public Chapter()
{
this.Assets = new HashSet<Asset>();
}
public int Id { get; set; }
public string Title { get; set; }
public int ModuleId { get; set; }
public string Notes { get; set; }
public short SortOrder { get; set; }
public System.DateTime CreateDate { get; set; }
public bool IsDeleted { get; set; }
public virtual Module Module { get; set; }
public virtual ICollection<Asset> Assets { get; set; }
}
}