如何在一个数据库的视图中显示多个表?

时间:2012-08-30 03:58:37

标签: asp.net database asp.net-mvc-3

目前我有一个包含以下字段的产品数据库表:

名称 类别(例如产品类型) 子类别(例如包装) 价

E.g类别A有3种包装。我希望它在三个不同的表中显示。我可以单独列出三张表,但表格显示相同的数据。

有人可以帮我吗?

我的公共ViewResult Index()控制器类中有这些代码。 var productsCornIndustrial =来自db.Products的cornIndustrial                                          其中cornIndustrial.Category ==“玉米油”& cornIndustrial.SubCategory ==“工业”                                          选择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());

1 个答案:

答案 0 :(得分:0)

了解ViewModels模式。我会这样写:

构建一个单独的ViewModel类,它将保存您的所有数据:

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
}

...

并在控制器中使用此类将视图传递给强类型的ViewModel对象:

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