嵌套模型在视图中返回null
本文关键字:返回 null 视图 模型 嵌套 | 更新日期: 2023-09-27 18:04:10
我知道这个问题之前已经问过了,但是我正在努力找到一个合适的解决方案。我有一个foreach loop
,比较2个模型的值,并决定每个父元素属于哪个,你可以在我的视图中看到(表有扩展/关闭能力),所以我希望每个组出现在他们已经分配给的部分下,但是当我调用item.Group.GroupSectionID
时,它每次都返回null,但是在调试控制器填充GroupDetail
模型和SectionDetail
模型时,public GroupDetail Group { get; set; }
返回null:
视图(SectionTable)
<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>
<<p> 控制器/strong> 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; }
}
提前感谢,如果您需要任何进一步的信息,请询问。
此处不能指定Group属性:
var sectionDetails = from u in data3
select new SectionDetail
{
SectionID = u.Id,
Name = u.Name,
Description = u.Description,
Group = u.Group
};