0

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

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

例如,A 类有 3 种包装。我希望它显示在三个不同的表格中。我可以分别取出三个表,但表显示相同的数据。

有人可以帮我吗?

我的公共 ViewResult Index() 控制器类中有这些代码。var productsCornIndustrial = from cornIndustrial in db.Products where cornIndustrial.Category == "Corn Oil" & cornIndustrial.SubCategory == "Industrial" 选择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());
4

1 回答 1

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);
}
于 2012-09-04T12:22:35.910 回答