2012-07-12 179 views
1

我想在我的視圖中顯示hierarchial數據顯示Hierarchial數據 - 我有2款(父/子鏈接)如何在MVC Razor視圖

型號:

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 

這將只填充診所數據的第一級 - 不是孩子屬性,或孩子房間屬性。

任何人都可以請幫我修理我的控制器或我的看法?

謝謝,馬克

回答

0

我已經加了1'd上面的答案,因爲它們非常有用 - 但實際問題/答案在我的模型定義中 - 要訪問控制器/視圖中的鏈接表,我必須將VIRTUAL添加到鏈接的屬性:

我在那裏:

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

本來應該:

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

我希望這可以幫助別人解決這個問題。

感謝Steve和Ufuk的幫助和時間。

馬克

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() 
          }); 
+0

嗨 - 謝謝你 - 我嘗試了上面的查詢,但與錯誤迎了上去: - \t \t實體或複雜類型「ttp.Models.Clinic」不能在LINQ被構造爲實體查詢「} \t System.SystemException {System.NotSupportedException} – Mark 2012-07-12 13:18:35

+0

@fixit然後,你應該在你的查詢中選擇c變量,EF應該填充值本身,但是你必須確保屬性被正確映射 – 2012-07-12 13:21:23

+0

Hi-VS now建議:無法找到類型或名稱空間'Select'(您是否缺少使用指令或程序集引用?) – Mark 2012-07-12 13:32:29

2

你不是真的返回診所的名單,因爲你使用

select new { ... } 

相反,你只想得到診所。模型和集合已經存在。有沒有必要重新創建它:

var ovw = db.Clinics; 

如果你想擁有的是從DB模式(這是很好的做法)單獨視圖模型看看AutoMapper或手動從DB診所映射到一個視圖模特診所。然後,而不是選擇新的{...}你會使用

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

其中vmClinic是您的viewmodel診所。

+0

對不起,我的帖子與Ufuk同時發佈,但是,是的! – 2012-07-12 11:59:36