2016-09-10 8 views
0

リポジトリパターンを使用してASP.NET MVCでの複数のテーブルからデータを取得) { this.Products = new HashSet(); }Entity Frameworkの私は私のモデルに関連した2つのクラスを持っている

public int ProviderId { get; set; } 
    public string OfficialName { get; set; } // nome usado no licenciamento 
    public string PopularName { get; set; } // nome popular, mais conhecido 
    public int Nuit { get; set; } //identificacao tributaria 
    public EstablishmentCategory EstablishmentCategory { get; set; } // tipo de estabelecimento 
    public HalalState HalalState { get; set; } 
    public DateTime? LastUpdate { get; set; } // date e hora do registo 

    public ICollection<Product> Products { get; set; } // lista de produtos halal    
} 

私はカミソリのエンジンを使用してページに表示するために、そのプロバイダ名と一緒に製品をロードしようとしています。ような何か:

Product Name (from table A) | Provider Name (from table B) 
Soft drink     | Coca-Cola 
Chips      | Lays 

クラスProductsRepositoryの製品を取得する方法:

public ICollection<Product> ReadAll() 
{  
    return context.Products.ToList(); 
} 

私はProductの他のプロパティと一緒にそれを表示するためには、ProviderクラスのOfficialNameプロパティに移動する方法が必要ですビューで。

答えて

1

ProductクラスにProviderタイプのナビゲーションプロパティを追加する必要があります。

public class Product 
{ 
    // Your existing properties goes here 

    public int ProviderID { get; set; } 
    public Provider Provider { set;get;} 
} 

今、あなたはビューに製品のリストを渡し、ビューで、その後通過するときにループ

ビュー

@model IEnumerable<Product> 
<table> 
    <tr> 
     <th>Name</th> 
     <th>Provider</th> 
    </tr> 
    @foreach(var item in Model) 
    { 
    <tr><td>@item.Name</td><td>@item.Provider.OfficialName</td></tr> 
    } 
</table> 
で今
public ActionResult Items() 
{ 
    var data = yourDbContext.Products.ToList(); 
    return View(data); 
} 

を、製品のプロパティを使用することができます

関連する問題