【问题标题】:generate a list of products of a category生成一个类别的产品列表
【发布时间】:2020-04-26 18:38:59
【问题描述】:

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

    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.net asp.net-mvc entity-framework


    【解决方案1】:

    在您的模型中,您添加了 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;
    

    【讨论】:

    • 如果您想知道导航属性是什么,这些是导航属性的示例; public virtual ProductTemplateModel ProductTemplate { get; set; } 和 public virtual CategoryModel Category{ get; set; }
    • 非常感谢杰迪诺。你能解释一下如何从导航属性中获取一个属性,例如在视图包中使用它的类别名称吗?
    • @Arya 例如你有一个 productModel 对象,你可以做string categoryName = productModel.ProductTemplate.Category.CategorytUrl;。我更新了答案。
    • 你会在你的控制器中包含你显示类别名称的地方吗?
    • 整个问题的原因是我想为每个类别创建一个页面并显示该类别的产品。我认为您提到的控制器包含在问题中,如果没有,请解释我必须包含什么控制器。我有点困惑。
    猜你喜欢
    • 2017-06-04
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    相关资源
    最近更新 更多