嵌套模型在视图中返回 null

Nested Model returning null in View

我知道之前有人问过这个问题,但我正在努力寻找合适的解决方案。我有一个 foreach loop 比较两个模型的值并决定每个模型属于哪个父元素,正如您在我的视图中看到的那样(Table 具有 expand/close 能力)所以我希望每个组都出现在它们被分配到的部分下,但是当我每次调用 item.Group.GroupSectionID 它 returns null 时,但是在调试控制器时填充 GroupDetail 模型和 SectionDetail 模型正在填充,但 public GroupDetail Group { get; set; } 返回 null:

查看(章节Table)

<table class="table table-striped">
    <tbody>
    <!-- Render the details of each employee. -->
    @foreach (var item in Model)
    {
    <tr>
        <td>-</td>
        <td>@Html.DisplayFor(model => item.Name)</td>
    </tr>

    if (item.Group.GroupSectionID != 0 && item.SectionID == item.Group.GroupSectionID)
    {
    <tr>
        <td colspan="3"><p>@Html.DisplayFor(model => item.Group.GroupName)</p></td>
    </tr>
    }
    }

    </tbody>
</table>

控制器

public ActionResult GroupTable()
{
    Manager manager = new Manager();
    var data1 = manager.GetAllGroups();
    var groupDetails = from u in data1
    select new GroupDetail
    {
        GroupID = u.Id,
                GroupDescription = u.Description,
                GroupName = u.Name,
                GroupSectionID = u.SectionId,
                SectionName = u.SectionName
    };
    return View(groupDetails.ToList());
}

public ActionResult SectionTable()
{
    Manager manager = new Manager();
    var data3 = manager.GetAllSections();
    var sectionDetails = from u in data3
    select new SectionDetail
    {
        SectionID = u.Id,
                Name = u.Name,
                Description = u.Description
    };
    return View(sectionDetails.ToList());
}

型号

public class SectionDetail
{
    public int SectionID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public GroupDetail Group { get; set; }

}

public class GroupDetail
{
    public int GroupID { get; set; }
    public string GroupDescription { get; set; }
    public string GroupName { get; set; }
    public int GroupSectionID { get; set; }
    public string SectionName { get; set; }
}

在此先感谢,如果您需要任何进一步的信息,请询问。

您没有在此处分配组 属性:

var sectionDetails = from u in data3
                     select new SectionDetail
                               {
                                   SectionID = u.Id,
                                   Name = u.Name,
                                   Description = u.Description,
                                   Group = u.Group
                               };