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 クラスのインスタンスを作成する必要があります。匿名型を使用しています。

IEnumerable<Property>また、クエリがorを返すため、Properties に対して ToList() を呼び出すことを忘れないでください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 クリニックからビュー モデル クリニックに手動でマップします。次に 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 に答える