如何在MVC Razor视图中显示分层数据

时间:2012-07-12 11:39:41

标签: asp.net asp.net-mvc asp.net-mvc-3 razor

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

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

任何人都可以帮我修理我的控制器或我的观点吗?

谢谢,Mark

3 个答案:

答案 0 :(得分:4)

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

也不要忘记为属性调用ToList(),因为您的查询将返回IEnumerable<Property>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()
                          });

答案 1 :(得分:2)

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

select new { ... }

相反,你想要去诊所。模型和集合已经存在。没有必要重新创建它:

var ovw = db.Clinics;

如果您正在尝试使用与db模型分开的视图模型(这是一个好习惯),请查看AutoMapper或手动映射db Clinic到视图模型Clinic。然后,而不是选择新的{...},你将使用

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

其中vmClinic是您的viewmodel诊所。

答案 2 :(得分:0)

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

我在哪里:

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

应该是:

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

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

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

标记

相关问题