Hello everybody. I'm a new member of Nop forum and i've problem to show Categories and it's subcategories in Home page. I'm using Nop 2.65. This is my code in View/Catalog/HomepageCategories.cshtml:
Hello everybody. I'm a new member of Nop forum and i've problem to show Categories and it's subcategories in Home page. I'm using Nop 2.65. This is my code in View/Catalog/HomepageCategories.cshtml:
Under CategoryNavigation(int currentCategoryId, int currentProductId) on CategoryController.cs, add those lines right before "return model". It should be working for version 2.65, 2.70 and 2.80.
foreach (var cat in model.Categories) { cat.SubCategories = _categoryService.GetAllCategoriesByParentCategoryId(cat.Id) .Select(x => { var subCatName = x.GetLocalized(y => y.Name); var subCatModel = new CategoryNavigationModel.CategoryModel()// CategoryModel.SubCategoryModel() { Id = x.Id, Name = subCatName, SeName = x.GetSeName(), }; return subCatModel; }).ToList(); } return model;
Hi, I was wondering if you could help me do the same for the PictureModel of CategoryModel? I need the picture from the picture model but i dont know how to populate it. Best regards, Gudrun
Under CategoryNavigation(int currentCategoryId, int currentProductId) on CategoryController.cs, add those lines right before "return model". It should be working for version 2.65, 2.70 and 2.80.
foreach (var cat in model.Categories) { cat.SubCategories = _categoryService.GetAllCategoriesByParentCategoryId(cat.Id) .Select(x => { var subCatName = x.GetLocalized(y => y.Name); var subCatModel = new CategoryNavigationModel.CategoryModel()// CategoryModel.SubCategoryModel() { Id = x.Id, Name = subCatName, SeName = x.GetSeName(), }; return subCatModel; }).ToList(); } return model;
This works for nop 3.10 as well.
Look at code placement below:
//"CurrentCategoryId" property of "CategoryNavigationModel" object depends on the current category or product. //We need to clone the cached model (the updated one should not be cached) var model = (CategoryNavigationModel)cachedModel.Clone(); model.CurrentCategoryId = activeCategoryId;
//-- ADDED CODE FROM FORUM (Below) foreach (var cat in model.Categories) { cat.SubCategories = _categoryService.GetAllCategoriesByParentCategoryId(cat.Id) .Select(x => { var subCatName = x.GetLocalized(y => y.Name); var subCatModel = new CategoryNavigationModel.CategoryModel()// CategoryModel.SubCategoryModel() { Id = x.Id, Name = subCatName, SeName = x.GetSeName(), }; return subCatModel; }).ToList(); } //-- ADDED CODE FROM FORUM (Above)
Paste this code into the CatalogController after line 1401 and before return PartialView(model)
foreach (var cat in model.Categories) { model.Categories = _categoryService.GetAllCategoriesByParentCategoryId(cat.Id) .Select(x => { var subCatName = x.GetLocalized(y => y.Name); var subCatModel = new CategorySimpleModel { Id = x.Id, Name = subCatName, SeName = x.GetSeName(), }; return subCatModel; }).ToList(); }
Hello i tried this approach but for some reason it's not working. In my case i have few groups of categories(not more than 4) and one of this has no sub-cateogory.
I just want to show everything expanded into the home page.
I already change the Catalog controller to GetAllCategoriesByParentCategoryId.
Do I need to change some view(maybe HomepageCategories)?