How to show Categories and it's subcategories in Home page Nop 2.65

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 anos atrás
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:

<div class="home-page-category-grid">
@foreach (var item in Model)
{
  <div class="item-box">
    <div class="category-item">
          <h2 class="title">
            <a href="@Url.RouteUrl("Category", new { categoryId = item.Id, SeName = item.SeName })" title="@item.PictureModel.Title">
                    @item.Name</a>
          </h2>
          <div class="picture">
          <a href="@Url.RouteUrl("Category", new { categoryId = item.Id, SeName = item.SeName })" title="@item.PictureModel.Title">
                                <img alt="@item.PictureModel.AlternateText" src="@item.PictureModel.ImageUrl"
                                    title="@item.PictureModel.Title" /></a>
      
          </div>        
        <div>
        @if(item.SubCategories.Count > 0){    
          foreach(var cat in item.SubCategories)
          {
          <li>@cat.Name</li>
          }
        }
        </div>
    </div>
  </div>
}  
</div>

But when i run website only Categories show on home page without it's subcategories.
Appreciate your help!
11 anos atrás
trangpoo wrote:
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:

<div class="home-page-category-grid">
@foreach (var item in Model)
{
  <div class="item-box">
    <div class="category-item">
          <h2 class="title">
            <a href="@Url.RouteUrl("Category", new { categoryId = item.Id, SeName = item.SeName })" title="@item.PictureModel.Title">
                    @item.Name</a>
          </h2>
          <div class="picture">
          <a href="@Url.RouteUrl("Category", new { categoryId = item.Id, SeName = item.SeName })" title="@item.PictureModel.Title">
                                <img alt="@item.PictureModel.AlternateText" src="@item.PictureModel.ImageUrl"
                                    title="@item.PictureModel.Title" /></a>
      
          </div>        
        <div>
        @if(item.SubCategories.Count > 0){    
          foreach(var cat in item.SubCategories)
          {
          <li>@cat.Name</li>
          }
        }
        </div>
    </div>
  </div>
}  
</div>

But when i run website only Categories show on home page without it's subcategories.
Appreciate your help!


In the source code, the mapping between Category (domain model) and CategoryModel (something like 'view model') reads:

        
public static CategoryModel ToModel(this Category entity)
        {
            if (entity == null)
                return null;

            var model = new CategoryModel()
            {
                Id = entity.Id,
                Name = entity.GetLocalized(x => x.Name),
                Description = entity.GetLocalized(x => x.Description),
                MetaKeywords = entity.GetLocalized(x => x.MetaKeywords),
                MetaDescription = entity.GetLocalized(x => x.MetaDescription),
                MetaTitle = entity.GetLocalized(x => x.MetaTitle),
                SeName = entity.GetSeName(),
            };
            return model;
        }


That means although CategoryModel has the 'SubCategory' property, it is not populated. You need to populate it yourself. :)
11 anos atrás
Thank you so much, Wooncherk.
But i'm using Nop 2.65 No source code. So, how do i do?
11 anos atrás
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;
10 anos atrás
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
10 anos atrás
ktvkong wrote:
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)

            return PartialView(model);
        }
10 anos atrás
The following code works for 3.2.

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();
            }
10 anos atrás
webie wrote:
The following code works for 3.2.

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)?

Thanks
10 anos atrás
See what I wrote here.  It was 2 years ago, but I think it still applies

https://www.nopcommerce.com/boards/t/11858/sub-cat-is-display-when-page-is-load.aspx
10 anos atrás
I would like to expand all the nodes from category and sub-domain categories.

For example i have three categories:

Notebooks
e-Books
GPs


and i want to show in home page.

Notebooks
    Item1
    ItemN
e-Books
    Item1
    ItemN
GPs
    Item1
    ItemN
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.