【问题标题】:how to get data from multiple related tables and pass it to the view mvc如何从多个相关表中获取数据并将其传递给视图 mvc
【发布时间】:2014-07-05 05:45:13
【问题描述】:

我的数据库类别和子类别和产品中有三个表:

类别有很多子类别 子类别有很多产品

所以我正在尝试加入这三个表以获取每个产品的数据并将其显示在这样的视图中:

 public ActionResult Index()
    {
        var memberId = WebSecurity.CurrentUserId;

        var productsCompany = db.Products
        .Join(db.SubCategorys, p => p.SubCategoryID, subcat => subcat.SubCategoryID,
                     (p, subcat) => new { p = p, subcat = subcat })
        .Join(db.Categorys, temp0 => temp0.subcat.CategoryID, cat => cat.CategoryID,
                     (temp0, cat) => new { temp0 = temp0, cat = cat })
        .Join(db.Sizes, temp1 => temp1.temp0.p.SizeID, s => s.SizeID,
                     (temp1, s) => new { temp1 = temp1, s = s })
        .Join(db.Colors, temp2 => temp2.temp1.temp0.p.ColorID, c => c.ColorID,
                     (temp2, c) => new { temp2 = temp2, c = c })
        .Join(db.Stores, temp3 => temp3.temp2.temp1.temp0.p.StoreID, st => st.StoreID,
                     (temp3, st) => new { temp3 = temp3, st = st })
        .Join(db.Companyies, temp4 => temp4.st.CompanyID, camp => camp.CompanyID,
                     (temp4, camp) => new { temp4 = temp4, camp = camp })
        .Where(temp5 => (temp5.camp.UserID == memberId))
        .Select(temp5 => new
        {
            CategoryName = temp5.temp4.temp3.temp2.temp1.cat.CategoryName,
            SubCategoryName = temp5.temp4.temp3.temp2.temp1.temp0.subcat.SubCategoryName,
            ProductImageURL = temp5.temp4.temp3.temp2.temp1.temp0.p.ProductImageURL,
            ProductName = temp5.temp4.temp3.temp2.temp1.temp0.p.ProductName,
            Price = temp5.temp4.temp3.temp2.temp1.temp0.p.Price,
            SizeName = temp5.temp4.temp3.temp2.s.SizeName,
            ColorName = temp5.temp4.temp3.c.ColorName,
            Quantity = temp5.temp4.temp3.temp2.temp1.temp0.p.Quantity,
            Sales = temp5.temp4.temp3.temp2.temp1.temp0.p.Sales,
            Discount = temp5.temp4.temp3.temp2.temp1.temp0.p.Discount,
            StoreName = temp5.temp4.st.StoreName,
            CompanyName = temp5.camp.CompanyName
        }).ToList();

        return View(productsCompany);
    }

但是这样获取数据需要时间,所以我尝试了另一种方式:

public ActionResult Index()
    {
        var memberId = WebSecurity.CurrentUserId;

        var productsCompany = db.Products.Include(p => p.Color).Include(p => p.Size).Include(p => p.Store).Include(p => p.SubCategory); 

        return View(productsCompany.ToList());
    }

但我不知道如何以这种方式从三表类别中获取数据,这仅用于在此索引视图中显示数据关于如何从这三个表中创建新产品的任何想法并感谢任何帮助

更新我的课程是:

产品类别

public class Product
{
    [ScaffoldColumn(false)]
    public int ProductID { get; set; }

    [DisplayName("Image URL")]
    //[DataType(DataType.Url)]
    public string ProductImageURL { get; set; }

    [Required(ErrorMessage = "Product Name is required")]
    [DisplayName("Product Name")]
    [StringLength(40)]
    public string ProductName { get; set; }

    [Required(ErrorMessage = "Price is required")]
    [DisplayName("Product Price")]
    [DataType(DataType.Currency)]
    [Range(1, 5000.00, ErrorMessage = "Price must be between 1 SP and 5000.00 SP")]
    public decimal Price { get; set; }

    [Required(ErrorMessage = "Quantity is required")]
    [DisplayName("Product Quantity")]
    public int Quantity { get; set; }

    [DisplayName("Sales Amount")]
    [Range(0, 100, ErrorMessage = "sale Prsent must be between 1 and 100")]
    public int Sales { get; set; }

    //Exclude
    public decimal Discount { get; set; }

    [Required(ErrorMessage = "Color is required")]
    [DisplayName("Color")]
    public int ColorID { get; set; }
    public virtual Color Color { get; set; }

    [Required(ErrorMessage = "Size is required")]
    [DisplayName("Size Type")]
    public int SizeID { get; set; }
    public virtual Size Size { get; set; }

    [Required(ErrorMessage = "Store is required")]
    [DisplayName("Store")]
    public int StoreID { get; set; }
    public virtual Store Store { get; set; }

    [Required(ErrorMessage = "Category Type is required")]
    [DisplayName("Sub Category Type")]
    public int SubCategoryID { get; set; }
    public virtual SubCategory SubCategory { get; set; }


}

子类别类:

 public class SubCategory
{

    [ScaffoldColumn(false)]
    public int SubCategoryID { get; set; }

    [Required(ErrorMessage = "Category Type is required")]
    [DisplayName("Category Type")]
    [StringLength(40)]
    public string SubCategoryName { get; set; }

    [Required(ErrorMessage = "Category is required")]
    [DisplayName("Category")]
    public int CategoryID { get; set; }
    public virtual Category Category { get; set; }
    public virtual ICollection<Product> Products { get; set; }
}

类别类:

 public class Category
{

    [ScaffoldColumn(false)]
    public int CategoryID { get; set; }

    [Required(ErrorMessage = "Category Name is required")]
    [DisplayName("Category Name")]
    [StringLength(50)]
    public string CategoryName { get; set; }
    public virtual ICollection<SubCategory> SubCategorys { get; set; }
}

【问题讨论】:

    标签: asp.net-mvc entity-framework asp.net-mvc-4 lambda entity-framework-5


    【解决方案1】:

    假设你的category、sub category和product table都与外键关联

    如果您使用 Entity Framework 访问数据库中的数据,并且 ProxyCreationEnabled 在您的 dbContext 类构造函数中设置为 true(默认情况下为 true),则该产品表将自动检索特定产品的相关类别和子类别数据。

    例如:

    Product objProduct =  _datacontext.Product.where(p=> p.productId.equals(pid));
    

    您的产品表定义如下:

    public class Product
    {
        public int productId {get; set;}
        ...
        public virtual IEnumarable<Category> pCategories {get; set;} 
        public virtual IEnumarable<SubCategory> pSubCategories {get; set;}
    }
    

    所以现在您的 objProduct 将自动将相关的 Category 和 SubCategory 分别存储在 pCategories 和 pSubCategories 中。您可以直接访问它,无需显式加入或包含相关表。

    现在将对象作为模型传递给视图

    public ActionResult Index()
    {
        Product objProduct =  _datacontext.Product.SingleOrDefault(p=> p.productId.equals(pid));
    
        return View(objProduct);
    }
    

    并根据需要在视图中使用模型。

    【讨论】:

    • 感谢您的回答,但我正在尝试获取数据库中的所有产品以及每个产品的相关子类别和类别,就像我在问题中提到的那样,我的类别有很多子类别子类别有许多产品我会更新问题以添加我的课程
    • 尝试获取var productList = _datacontext.Product.toList();
    猜你喜欢
    • 2011-07-26
    • 2018-12-08
    • 2020-11-05
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    相关资源
    最近更新 更多