2012-07-13 79 views
1

使用Nop Commerce我需要更改类别导航才能获得孩子。currentCategoryId = currentId变量int?

父类具有的categoryID和0的ParentCategoryID的儿童类别具有的categoryID和被映射到与自己的CategoryId的ParentCategoryID如下面的例子所示。

ID PCID NAME 
10 0 Computers 
11 10 Software 
12 10 Hardware 
13 0 Football 
14 13 Tottenham 
15 15 Manchester United 

目录控制器中有两种方法,一种是非行动和公共行为,如下所示。当我从子类别导航到产品时,导航视图会消失,但我希望它保留在产品连接的当前类别标识中,如果有意义的话。

[NonAction] 
    private IList<CategoryNavigationModel> GetOnlySiblings(Category currentCategory) 
    { 
     var result = new List<CategoryNavigationModel>(); 
     int Id = 0; 
     if (currentCategory != null) 
      Id = currentCategory.Id; 

     foreach (var category in _categoryService.GetAllCategoriesByParentCategoryId(Id)) 
     { 
      var model = new CategoryNavigationModel() 
      { 
       Id = category.Id, 
       Name = category.GetLocalized(x => x.Name), 
       SeName = category.GetSeName(), 
       IsActive = currentCategory != null && currentCategory.Id == category.ParentCategoryId, 
       NumberOfParentCategories = 0, 
      }; 

      result.Add(model); 
     } 
     return result; 
    } 

和公共行动

[ChildActionOnly] 
    //[OutputCache(Duration = 120, VaryByCustom = "WorkingLanguage")] 
    public ActionResult CategoryNavigation(int currentCategoryId, int currentProductId) 
    { 
     string cacheKey = string.Format(ModelCacheEventConsumer.CATEGORY_NAVIGATION_MODEL_KEY, currentCategoryId, currentProductId, _workContext.WorkingLanguage.Id); 
     var cacheModel = _cacheManager.Get(cacheKey,() => 
     { 
      var currentCategory = _categoryService.GetCategoryById(currentCategoryId); 
      if (currentCategory == null && currentProductId > 0) 
      { 
       var productCategories = _categoryService.GetProductCategoriesByProductId(currentProductId); 
       if (productCategories.Count > 0) 
        currentCategory = productCategories[0].Category; 
      } 
      var breadCrumb = currentCategory != null ? GetCategoryBreadCrumb(currentCategory) : new List<Category>(); 
      var model = GetOnlySiblings(currentCategory); 
      return model; 
     }); 

     return PartialView(cacheModel); 
    } 

回答

0

为什么不尝试重用现有的逻辑,这样的事情:

public IList<CategoryNavigationModel> GetOnlySiblings(int currentCategoryId) 
     { 
      var currentCategory = _categoryService.GetCategoryById(currentCategoryId); 
      var siblingCategories = GetChildCategoryNavigationModel(new List<Category>(), currentCategory.ParentCategoryId, currentCategory, 0); 

      return siblingCategories; 
     }