2017-04-06 14 views
0

ASP.NET MVC EFはかなり新しくなっています私は、作成したユーザーを、名前、職場住所、自宅住所、資産名などのインデックスビューに作成するactionresult関数で表示しようとしています。ASP.NET MVCアグリゲーションデータを表示する方法

これまでのところ、

public ActionResult Index() 
{ 
     var clients = db.Clients.Include(c => c.HomeAddress); 
     return View(clients.ToList()); 
} 

資産名、職場住所などの他のデータも表示するにはどうすればよいですか。

私のクライアントモデル:

public class Client : Person { 
    public ICollection<OccupancyHistoryRecord> OccupancyRecords { get; set;  } 

     public ICollection<RentHistoryRecord> RentRecords { get; set; } 

     public Asset Assets { get; set; } 
    } 
} 

人モデル:

public class Person { 
    public int Id { get; set; } 

    [Required] 
    public string Name { get; set; } 

    [Display(Name = "Home Address")] 
    public FullAddress HomeAddress { get; set; } 

    [Display(Name = "Work Address")] 
    public FullAddress WorkAddress { get; set; } 
} 

FullAddressモデル:

public Class FullAddress { 
    public int Id { get; set; } 

    [Display(Name = "Suite")] 
    public string UnitNum { get; set; } 

    [Display(Name = "Street Address")] 
    public string StreetAddress { get; set; } 

    public string Province { get; set; } 

    public string Country { get; set; } 

    [Display(Name = "Postal Code")] 
    public string PostalCode { get; set; } 
} 

資産モデル:

public Class Asset { 
    public int Id { get; set; } 

    [Display(Name = "Asset Name")] 
    public string Name { get; set; } 

    [Display(Name = "Asset Type")] 
    public string Type { get; set; } 

    public FullAddress Address { get; set; } 

    [Display(Name = "Asking Rent")] 
    public string AskingRent { get; set; } 

    public ICollection<OccupancyHistoryRecord> OccupancyRecords; 

    public ICollection<RentHistoryRecord> RentRecords; 

} 
これまで

私のインデックス表示:すべてのヘルプは高く評価され

@model IEnumerable<RentalManagement.Models.Client> 

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 
    <table class="table"> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.Name) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Assets.Name) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Assets.Type) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.HomeAddress) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.WorkAddress) 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.Name) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Assets.Name) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Assets.Type) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.HomeAddress.StreetAddress) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.WorkAddress) 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "Edit", new { id=item.Id }) | 
      @Html.ActionLink("Details", "Details", new { id=item.Id }) | 
      @Html.ActionLink("Delete", "Delete", new { id=item.Id }) 
     </td> 
    </tr> 
} 

。ありがとう!

EF Coreバージョン:

public ActionResult Index() 
{ 
    var clients = db.Clients 
     .Include(p => p.Assets) 
      .ThenInclude(p => p.Address) 
     .Include(p => p.Assets) 
      .ThenInclude(p => p.OccupancyRecords) 
     .Include(p => p.Assets) 
      .ThenInclude(p => p.RentRecords) 
     .Include(p => p.HomeAddress) 
     .Include(p => p.WorkAddress) 
     .ToList(); 
    return View(clients); 
} 

EF 6版:

public ActionResult Index() 
{ 
    var clients = db.Clients 
     .Include(p => p.Assets.Select(s => s.Address)) 
     .Include(p => p.Assets.Select(s => s.OccupancyRecords)) 
     .Include(p => p.Assets.Select(s => p.RentRecords)) 
     .Include(p => p.HomeAddress) 
     .Include(p => p.WorkAddress) 
     .ToList(); 
    return View(clients); 
} 

はまた、遅延ロードを有効にするには、仮想として、このプロパティをマーク

答えて

1

は、すべての仮想プロパティを含めますメカニズム:

public virtual ICollection<OccupancyHistoryRecord> OccupancyRecords; 
public virtual ICollection<RentHistoryRecord> RentRecords; 
+0

私のためにThenInclude関数がないようですか? –

+0

'.ThenInclude'はEFコア拡張です。私は自分の答えを編集し、EF 6版も投稿しました。 –

+0

ああ大丈夫、これはうまくいきました。 –

関連する問題