2011-09-08 53 views
2

我有一個基於MVC音樂商店教程創建的MVC應用程序,而不是音樂商店,我正在構建時尚&服裝店。我更改了部分視圖以顯示品牌(音樂商店中的藝術家)和類別(流派),產品必須按性別過濾。我嘗試過不同的方法,但沒有接近我的期望。根據條件檢索類別及其相關產品

下面是從StoreController樣本:

public ActionResult Brand(int cod) 
    { 
     // Retrieve Brand and its associated Products from database 
     var brandModel = storeDB.Brands.Include("Products") 
      .Single(g => g.BrandId == cod); 

     return View(brandModel); 
    } 

我想要得到的東西是這樣的:

/存儲/瀏覽/文類型= 1
/存儲/瀏覽/女人?Type = 1
/Store/Browse/Man?Brand = 1
/Store/Browse/Woman?Brand = 1

菜單會...

  • 女人
    • 瀏覽依風格
      • [類別]
    • 瀏覽依品牌
      • [BRANDS]
    • 瀏覽依風格
      • [類別]
    • 瀏覽依品牌
      • [BRANDS]

品牌可以有男女皆宜的產品,但從/Man?Brand=1瀏覽時,應該只顯示男士服裝,如果一個品牌只有女士服裝,該品牌不應該在Man/Browse by Brand菜單上列出。

我需要更改ActionResults才能驗證產品表上的性別列?

+0

這實際上是一個數據問題,而不是一個MVC問題。你在使用Linq還是EF?這將有助於確定此問題的正確標記。 – counsellorben

+0

對不起,我忘了補充說我正在使用實體框架。 – Samuel

回答

0

如果我們使用下面的簡化模型:

public class Brand 
{ 
    public int BrandId { get; set; } 
    public string BrandName { get; set; } 
    // other properties 
} 

public class Category 
{ 
    public int CategoryId { get; set; } 
    public string CategoryName { get; set; } 
    // other properties 
} 

public class Product 
{ 
    public int ProductId { get; set; } 
    public int BrandId { get; set; } 
    public int CategoryId { get; set; } 
    public string ProductGender { get; set; } 
    // other properties 
} 

然後我們可以創建不同性別的類別如下:

修訂

var womenByStyle = (from p in _db.Products where p.ProductGender == "Female" 
    join c in _db.Categories on c.CategoryId equals p.CategoryId 
    select c.CategoryId, c.CategoryName).Distinct(); 

var womenByBrand = (from p in _db.Products where p.ProductGender == "Female" 
    join b in _db.Brand on b.BrandId equals p.BrandId 
    select b.BrandId, b.BrandName).Distinct(); 

我們可以做具體的搜索在我們的查詢字符串中(如Man?Brand=1)如下:

public ActionResult Man(int? Brand, int? Style) 
{ 
    if (Brand != null) 
    { 
     var brandProductsForMale = from p in _db.Products 
      where p.ProductGender == "Male" && BrandId == Brand; 

     // map to ViewModel 

     return View("Products", brandProductsForMaleModel); 
    } 

    // et cetera 
} 
+0

試過這個,並得到一些錯誤。我應該在哪裏放女裝女裝,女裝品牌等?我把我的StoreController()和c.CategoryId(名稱c不在等於左邊的範圍),select(預期的上下文keywork等於),distinct(在當前上下文中不存在) 。在ActionResult上,我收到「where」(代表不接受1個參數)的錯誤... – Samuel

+0

Sprite,對不起,如果我通過LINQPad運行LINQ語句,我會看到我的錯誤。以上是更正後的LINQ語句。 – counsellorben

+0

這裏仍然有一些錯誤,「在聲明之前不能使用局部變量c」,「名稱p不在等於右邊的範圍」,並且在select語句上,選擇c.CategoryId,c.CategoryName「 ;預計「 – Samuel

相關問題