1

使用 Nop Commerce 我需要更改类别导航以获取 childsiblingsonly。

父类别的 categoryID 和 ParentCategoryID 为 0。子类别具有 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 

目录控制器中有两种方法,如下所示的非操作和公共操作。当我从子类别导航到产品时,导航视图会消失,但如果有意义,我希望它保留在产品连接到的 currentCategory Id 上。

[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);
    }
4

1 回答 1

0

为什么不尝试重用现有逻辑,如下所示:

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

            return siblingCategories;
        }
于 2012-11-20T12:51:47.333 回答