我是初学者,我希望在左侧显示菜单,如This Website,当用户点击任何类别名称或其子类别名称时,我想显示与所点击类别相关的产品。
我在我的数据库中创建了4个表。
分类
CategoryId(pk),CategoryName,Description,Icon
子类别
SubCategoryId(pk),SubCategoryName,Description,Icon,CategoryId(fk)
SubSubCategory
SubSubCategoryId(pk),SubSubCategoryName,Description,Icon,SubCategoryId(fk)
产品
ProductId(pk),名称,价格,描述,CategoryId(fk),SubCategoryId(fk),SubSubCategoryId(fk)
Category.cs模型
public partial class Category
{
public Category()
{
Products = new HashSet<Product>();
SubCategories = new HashSet<SubCategory>();
}
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
public string Icon{ get; set; }
public virtual ICollection<Product> Products { get; set; }
public virtual ICollection<SubCategory> SubCategories { get; set; }
}
SubCategory.cs
public partial class SubCategory
{
public SubCategory()
{
Products = new HashSet<Product>();
SubSubCategories = new HashSet<SubSubCategory>();
}
[Key]
public int SubCategoryId { get; set; }
public string SubCategoryName { get; set; }
public int? CategoryId { get; set; }
public string Description { get; set; }
public string Icon{ get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<Product> Products { get; set; }
public virtual ICollection<SubSubCategory> SubSubCategories { get; set; }
}
SubSubCategory.cs
public partial class SubSubCategory
{
public SubSubCategory()
{
Products = new HashSet<Product>();
}
[Key]
public int SubSubCategoryId { get; set; }
public string SubSubCategoryName { get; set; }
public int? SubCategoryId { get; set; }
public string Description { get; set; }
public string Image { get; set; }
public virtual ICollection<Product> Products { get; set; }
public virtual SubCategory SubCategory { get; set; }
}
Product.cs
public partial class Product
{
public int ProductId { get; set; }
public int? CategoryId { get; set; }
public int? SubCategoryId { get; set; }
public int? SubSubCategoryId { get; set; }
public string Name { get; set; }
[AllowHtml]
public string Description { get; set; }
public decimal? Price { get; set; }
}
答案 0 :(得分:0)
您应该尝试首先加载要在当前视图中使用的整个集合。
然后使用lambda表达式或谓词将数据分类为类别,例如
var list = wholeList.Where(i => i.category == "{category-name}");
然后将此列表加载到视图模板。
这种方法既快捷又方便
答案 1 :(得分:-1)
点击类别,子类别,子子类别, 你应该尝试下面的MVC方式加载部分。使用适当的重载。
@Ajax.ActionLink( ....)