如何从多个相关表中的数据,并把它传递给视图MVC(how to get data from mult

2019-10-20 09:41发布

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

种类有很多子目录子目录有许多产品

所以我想加盟此三个表来获取数据的每个产品在这样的一个视图中显示它:

 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());
    }

但我无法弄清楚如何从这样的threed表类别获得数据,这仅用于显示在该指数中的数据查看有关如何从这个三个表,并感谢所有帮助创建一个新的产品任何想法

更新我的课是:

产品类别

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; }
}

Category类:

 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; }
}

Answer 1:

假设您的类别,子类别和产品表与forign键关联

如果您正在使用实体框架从数据库中访问数据和ProxyCreationEnabled设置为true(默认为true)在你的DbContext类的构造函数,该产品表将自动检索特定产品相关的类别和子类别的数据。

例如:

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将自动存储在pCategories和pSubCategories相关的类别和子类别分别。 你可以明确地直接访问它, 无需加入或Inclue相关的表。

现在传递对象的视图作为一个模型

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

    return View(objProduct);
}

和使用模型视图中按规定。



文章来源: how to get data from multiple related tables and pass it to the view mvc