2012-08-30 30 views
0

目前,我有這些領域產品數據庫表:如何在一個數據庫的視圖中顯示多個表?

名稱 類別(如產品類型) 子目錄(如包裝) 價格

E.g A類有3種包裝。我希望它在三個不同的表格中顯示。我可以單獨得到三張表,但表格顯示的是相同的數據。

有人可以幫我嗎?

我在我的公共ViewResult指數()控制器類中有此代碼。 var productsCornIndustrial = from cornIndustrial in db.Products where corn cornIndustrial.Category ==「Corn Oil」& cornIndustrial.SubCategory ==「Industrial」 select cornIndustrial;

 return View(productsCornIndustrial.ToList()); 

     var productsCornFoodservice = from cornFoodservice in db.Products 
             where cornFoodservice.Category == "Corn Oil" & cornFoodservice.SubCategory == "Foodservice" 
             select cornFoodservice; 

     return View(productsCornFoodservice.ToList()); 

     var productsCornRetail = from cornRetail in db.Products 
           where cornRetail.Category == "Corn Oil" & cornRetail.SubCategory == "Retail" 
           select cornRetail; 

     return View(productsCornRetail.ToList()); 

回答

0

閱讀關於ViewModels模式。我會寫這樣的:

建立一個獨立的視圖模型類,將保存所有數據:

public class NewViewModel 
{ 
    public List<Products> IndustrialList {get;set;} 
    public List<Products> FoodList {get;set;} 
    public List<Products> RetailList{get;set;} 

    //ctor etc... get the data from DAL 
} 

....

,並在控制器中使用這個類來傳遞TOT視圖一個強類型的ViewModel對象:

public ActionResult Index(int? id) 
{ 
    var items = new NewViewModel(); 
    return View(items); 
} 
+0

那麼我在哪裏把我的數據庫代碼? – sling

+0

那麼,我正在爲同一個解決方案中的Web應用程序創建3個項目。 1)核心 - 類庫,我有DAL代碼和存儲庫2)用戶界面 - MVC應用程序3)單元測試。因此,您可以將所有代碼添加到單獨的組件中,並將其組織在其中,然後在控制器中添加組件 –

相關問題