2011-11-14 93 views
1

我在我的MVC應用程序上有一個多級下拉菜單,我試圖按品牌,性別和類型對產品進行排序。我的菜單看起來像這樣:Lambda表達式只選擇包含某些產品的類別

<li>Man 
      <ul> 
       <li><h2>By Type</h2></li> 
       <li>CATEGORY</li> 
      </ul> 
      <ul> 
       <li><h2>By Brand</h2></li> 
       <li>BRAND</li> 
      </ul> 
     </div> 
    </li> 
    <li> 
    <li>Woman 
      <ul> 
       <li><h2>Categories</h2></li> 
       <li>CATEGORY</li> 
      </ul> 
      <ul> 
       <li><h2>Brands</h2></li> 
       <li>BRAND</li> 
      </ul> 
     </div> 
    </li> 
    <li> 

,這裏是我的控制器之一:

public ActionResult Man(int type) 
    { 
     var productTypeModel = storeDB.Categories.Include("Products") 
      .Single(g => g.CategoryId == type); 

     return View(productTypeModel); 
    } 

這是加載到菜單partialview:

@model IEnumerable<Store.Models.Category> 
<ul> 
<li><h2>By Type</h2></li> 
@foreach (var type in Model) 
{ 
    <li>@Html.ActionLink(type.Name, 
      "Man", "Store", 
      new { Category = type.CategoryId }, null) 
    </li> 
} 
</ul> 

有沒有什麼辦法,我只能使用lambda表達式顯示具有與每個性別相關的產品的類別和品牌?我的意思是,當我瀏覽男士產品時,我不希望「裙子」出現在「按類型」上,我不希望男士產品出現在女士區域。

回答

1

你可以使用的GroupBy:

public ActionResult Menu() 
{ 
    var model = 
     from p in storeDB.Products.Include("Category").Include("Type") 
     group p by p.Gender 
     into genderGroup 
     select new MenuObject 
     { 
      Gender = genderGroup.Key, 
      Categories = 
      (
       from p2 in genderGroup 
       group p2 by p2.Category 
       into categoryGroup 
       select categoryGroup.Key 
      ), 
      Types = 
      (
       from p3 in genderGroup 
       group p3 by p3.Type 
       into typeGroup 
       select typeGroup.Key 
      ) 
     }; 

    return View(model); 
} 

public class MenuObject 
{ 
    public string Gender { get; set; } 
    public IEnumerable<Category> Categories { get; set; } 
    public IEnumerable<Type> Types { get; set; } 
} 

然後在模板:

@foreach (var item in Model) 
{ 
    <li>@item.Gender 
     <ul> 
      <li><h2>By Category</h2></li> 
      @foreach (var category in item.Categories) 
      { 
       <li>@Html.ActionLink(category.Name, 
         "ByCategory", "Store", 
         new { 
          Gender = item.Gender, 
          Category = category.ID, 
         }, null)</li> 
      } 
     </ul> 
     <ul> 
      <li><h2>By Type</h2></li> 
      @foreach (var type in item.Types) 
      { 
       <li>@Html.ActionLink(type.Name, 
         "ByType", "Store", 
         new { 
          Gender = item.Gender, 
          Type = type.ID, 
         }, null)</li> 
      } 
     </ul> 
    </li> 
} 

這將產生類似:

<li>Man 
    <ul> 
     <li><h2>By Category</h2></li> 
     <li><a href="/Store/ByCategory/Man/2/">Sports</a></li> 
     <li><a href="/Store/ByCategory/Man/3/">Underwear</a></li> 
    </ul> 
    <ul> 
     <li><h2>By Type</h2></li> 
     <li><a href="/Store/ByType/Man/1/">Boxer Shorts</a></li> 
     <li><a href="/Store/ByType/Man/2/">Socks</a></li> 
    </ul> 
</li> 
<li>Woman 
    <ul> 
     <li><h2>By Category</h2></li> 
     <li><a href="/Store/ByCategory/Woman/1/">Running</a></li> 
     <li><a href="/Store/ByCategory/Woman/2/">Sports</a></li> 
     <li><a href="/Store/ByCategory/Woman/3/">Underwear</a></li> 
    </ul> 
    <ul> 
     <li><h2>By Type</h2></li> 
     <li><a href="/Store/ByType/Woman/2/">Socks</a></li> 
     <li><a href="/Store/ByType/Woman/3/">Bra</a></li> 
    </ul> 
</li> 
+0

哇!謝謝!這已經解決了我想在幾個月前解決的問題! – Samuel

相關問題