我正在尝试在视图中显示层次结构数据 - 我有 2 个模型(父/子链接)
楷模:
public class Clinic
{
public int ClinicId { get; set; }
[Display(Name="Clinic Name")]
public string Name { get; set; }
public string Description { get; set; }
public List<Property> Properties { get; set; }
}
public class Property
{
public int PropertyID { get; set; }
public int ClinicId { get; set; }
[Display(Name="Property Address")]
public string Address { get; set; }
public Clinic Clinic { get; set; }
public List<Room> Rooms { get; set; }
}
public class Room
{
public int RoomId { get; set; }
public int PropertyId { get; set; }
public string RoomName { get; set; }
public Property Property { get; set; }
}
我的控制器正在构建这个 Linq 查询 - 它在 LinqPad 中显示良好:
//
// GET: /Property/Overview/
public ActionResult Overview()
{
// original code - replaced following help below
// var ovw =
// (from c in db.Clinics
// select new
// {
// c.Name,
// Properties =
// from p in db.Properties
// where p.ClinicId == c.ClinicId
// select new
// {
// p.Address
// }
// });
IEnumerable<Clinic> ovw = from c in db.Clinics
select c;
return View(ovw);
}
我的看法是:
@model IEnumerable<ttp.Models.Clinic>
@{
ViewBag.Title = "Overview";
}
<h2>Overview</h2>
@foreach (var item in Model) {
@item.Name
<br />
if (item.Properties != null){
foreach (var item2 in item.Properties) {
@item2.Address <br />
if (item2.Rooms != null){
foreach (var item3 in item2.Rooms) {
@item3.RoomName <br />
}
}
}
}
它应该显示:
Clinic1
Property1
Room1
Room2
Room3
Property2
Room4
Room5
Clinic2
Property3
Room6
....
但只是显示:
Clinic1
Clinic2
Clinic3
这只会填充诊所的第一级数据 - 而不是子属性或属性的子房间。
谁能帮我修复我的控制器或我的视图?
谢谢,马克