首页 > 解决方案 > 生成一个类别的产品列表

问题描述

我正在开发一个商店应用程序,我需要展示每个类别的产品。问题是每个产品都是从存储在表中的产品模板创建的,并且每个模板都与一个类别相关。这是产品型号:

    namespace fardashahr.Models
{
    [Table("Product")]
    public class ProductModel
    {
        public ProductModel()
        {
            if (Specs == null)
            {
                Specs = new Dictionary<string, SpecItemsModel>();
            }
        }
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Required]
        public int ProductTemplateId { get; set; }

        [Required]
        public bool IsPublished { get; set; }

        [Required]
        public bool InStock { get; set; }

        [Range(95, 110)]

        public float SyncRate { get; set; }


        public DateTime? ProductionDate { get; set; }

        [Required]
        public DateTime RegisterationDate { get; set; }

        public string ImageName { get; set; }

        public IEnumerable<string> GalleryImages { get; set; }

        [NotMapped]
        public Dictionary<string, SpecItemsModel> Specs { get; set; }

        [ForeignKey("ProductTemplateId")]
        public virtual ProductTemplateModel ProductTemplate { get; set; }

        [ForeignKey("ManufacturerId")]
        public virtual CodingItemModel Manufacturer { get; set; }

        [ForeignKey("BrandId")]
        public virtual CodingItemModel Brand { get; set; }

        [ForeignKey("ModelId")]
        public virtual CodingItemModel Model { get; set; }

        [ForeignKey("SeriesId")]
        public virtual CodingItemModel Series { get; set; }
    }
}

这是ProductTemplate:

    namespace fardashahr.Models
{
    [Table("ProductTemplate")]
    public class ProductTemplateModel
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
        public int Id { get; set; }


        [StringLength(500)]
        public string Name { get; set; }

        [Required]  
        public int CategoryId { get; set; }


        [StringLength(500)]
        public string Description { get; set; }

        [ForeignKey("CategoryId")]
        public virtual CategoryModel Category{ get; set; }

    }
}

控制器是:

namespace fardashahr.Controllers
{
    public class ProductsController : Controller
    {
        // GET: Products
        public ActionResult Index()
        {
            return RedirectToAction("Index", "Home");
        }

        public ActionResult Category(string name)
        {
            //declare a list of products
            List<ProductModel> productList;

            using(MainModel db = new MainModel())
            {
                //get category id
                CategoryModel category = db.Categories.Where(x => x.CategorytUrl == name).FirstOrDefault();
                int catId = category.Id;

                //initialize the list
                productList = db.Products.Where(x => x. == catId).ToList();
            }



        }
    }
}

最后,我想知道的是如何初始化产品列表。

标签: asp.netasp.net-mvcentity-framework

解决方案


在您的模型中,您添加virtual了表示导航属性将自动加载而无需 LINQ lambda.include()表达式的关键字。

因此,您可以立即访问导航属性并像这样加载列表;

productList = db.Products.Where(x => x.ProductTemplate.CategoryId == catId).ToList();

string categoryNameOfFirstProduct = productList.FirstOrDefault().ProductTemplate.Category.Name;

string categoryNameOfFirstProduct = productList.FirstOrDefault().ProductTemplate.Category.CategorytUrl;

推荐阅读