2016-01-23 105 views
0

我是一名初學者,我想在左側顯示菜單,如This Website,當用戶單擊任何類別名稱或其子類別名稱時,我想顯示與此相關的產品點擊類別。Asp.Net中的數據庫動態菜單MVC

我在我的數據庫中創建了4個表格。

  1. 類別

    類別ID(PK),類別名稱,描述,圖標

  2. 子類別

    SubCategoryId(PK),SubCategoryName,描述,圖標,類別ID(FK)

  3. SubSubCategory

    SubSubCategoryId(PK),SubSubCategoryName,描述,圖標,SubCategoryId(FK)

  4. 產品

    產品編號(PK),名稱,價格,描述,類別編號(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; } 
} 

子Category.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

並且控制器和視圖在哪裏?你做了什麼? – lyz

+0

你應該先對自己的各種教程網站進行一些研究,然後自己嘗試一下。如果事情仍然沒有按照他們的想法工作,那就發佈一個問題。這看起來像你不想嘗試,並希望我們爲你做! –

+0

我搜索了這個,他們只在教程中使用了1個表格。像菜單表只有MenuId,MenuName,Url,ParentId。但我有3個菜單表 –

回答

-1

在點擊類別,子類別,子子類別中, 你應該嘗試低於MVC方式加載partials。使用適當的過載。

@Ajax.ActionLink(....) 
  • 記住在你的元素稱爲部分會加載使用目標ID。
  • 您的佈局必須包含jquery和jquery.unobtrusive-ajax。 (jQuery版本必須高於1.9)
0

您應該嘗試首先加載要在當前視圖中使用的整個集合。

然後使用lambda表達式或謂詞數據到類別e.g

var list = wholeList.Where(i => i.category == "{category-name}"); 

然後加載此列表視圖模板進行排序。

這種方法既快又方便