1

我正在尝试在视图中显示层次结构数据 - 我有 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

这只会填充诊所的第一级数据 - 而不是子属性或属性的子房间。

谁能帮我修复我的控制器或我的视图?

谢谢,马克

4

3 回答 3

4

您应该创建 Clinic 和 Property 类的实例。您正在使用匿名类型。

也不要忘记为 Properties 调用 ToList() ,因为您的查询将返回IEnumerable<Property>or IQueryable<Property>

IEnumerable<Clinic> ovw = from c in db.Clinics
                          select new Clinic
                          {
                            Name = c.Name,
                            Properties =(from p in db.Properties
                                         where p.ClinicId == c.ClinicId
                                         select new Property
                                         {
                                             Address = p.Address
                                         }).ToList()
                          });
于 2012-07-12T11:55:53.077 回答
2

您实际上并没有返回诊所列表,因为您使用过

select new { ... }

相反,您只想获得诊所。模型和集合已经存在。无需重新创建它:

var ovw = db.Clinics;

如果您尝试使用与 db 模型分离的视图模型(这是一种很好的做法),请查看 AutoMapper 或手动从 db Clinic 映射到视图模型 Clinic。然后,您将使用 select new { ... } 而不是

select new vmClinic { Name = c.Name, ... }

其中 vmClinic 是您的视图模型诊所。

于 2012-07-12T11:59:06.030 回答
0

我已经将上面的答案加了 1,因为它们非常有帮助 - 但实际的问题/答案在我的模型定义中 - 要访问控制器/视图中的链接表,我必须将 VIRTUAL 添加到链接属性:

我有的地方:

public List<Property> Properties { get; set; } 

那应该是:

public virtual ICollection<Property> Properties { get; set; }

我希望这可以帮助其他人解决这个问题。

感谢 Steve 和 Ufuk 的帮助和时间。

标记

于 2012-07-12T15:45:56.923 回答