我想在我的視圖中顯示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
這將只填充診所數據的第一級 - 不是孩子屬性,或孩子房間屬性。
任何人都可以請幫我修理我的控制器或我的看法?
謝謝,馬克
嗨 - 謝謝你 - 我嘗試了上面的查詢,但與錯誤迎了上去: - \t \t實體或複雜類型「ttp.Models.Clinic」不能在LINQ被構造爲實體查詢「} \t System.SystemException {System.NotSupportedException} – Mark 2012-07-12 13:18:35
@fixit然後,你應該在你的查詢中選擇c變量,EF應該填充值本身,但是你必須確保屬性被正確映射 – 2012-07-12 13:21:23
Hi-VS now建議:無法找到類型或名稱空間'Select'(您是否缺少使用指令或程序集引用?) – Mark 2012-07-12 13:32:29