2016-09-10 1 views
0

저장소 패턴을 사용하여 ASP.NET MVC에서 여러 테이블에서 데이터를 검색) { this.Products = new HashSet(); 은}엔티티 프레임 워크와 내 모델에 관련이 개 수업을

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 제품을 검색하는 방법 : Product의 또 다른 속성과 함께 그것을 표시하기 위해,

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

내가 Provider 클래스의 OfficialName 속성을 탐색 할 수있는 방법이 필요 보기에서.

답변

1

Product 클래스에 Provider 유형의 탐색 속성을 추가해야합니다.

public class Product 
{ 
    // Your existing properties goes here 

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

이제보기에 제품의 목록을 전달하고 제품 속성을 사용하면 뷰에서

public ActionResult Items() 
{ 
    var data = yourDbContext.Products.ToList(); 
    return View(data); 
} 

이제 뷰에서 다음을 통해 당신은 루프,

@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> 
관련 문제