ビューに階層データを表示しようとしています - 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
これにより、クリニックの最初のレベルのデータのみが入力されます。子プロパティやプロパティの子ルームではありません。
コントローラーまたはビューの修正を手伝ってくれる人はいますか?
ありがとう、マーク