I need to change the category navigation to get childsiblingsonly.
A parent category has a categoryID and a ParentCategoryID of 0. A child category has a categoryID and is mapped to the ParentCategoryID with its own CategoryId as seen in the example below.
ID PCID NAME
10 0 Computers
11 10 Software
12 10 Hardware
13 0 Football
14 13 Tottenham
15 13 Manchester United
There are two methods in the catalog controller a Non Action and Public Action seen below. When I navigate to the product from the child category the navigation view disappears but I would like it to remain on the currentCategory Id the product are connected to if makes sense.
[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;
}
and the public action
[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);
}
I guess it is hard to understand what you have implemeneted here, but suggest to use two partial view and refer them that way your one view will be constant and other will keep changing.
The main question is the variable operand operator. In the non action method. This code below is the area that needs cleaning I think.
var result = new List<CategoryNavigationModel>();
int Id = 0;
if (currentCategory != null)
Id = currentCategory.Id;
On passing the final Category.Id into the Product the partial view dissappears but I need it to remain. My question how do I keep it there with the right variable?
If I change the above code to
var result = new List<CategoryNavigationModel>();
int Id = 0;
if (currentCategory != null)
Id = 1;
All parentcategories direct themselves to categoryId of one - what I need HELP with is keeping the subcategories view in place when the products are reached.
wertyuio1
Member
67 Points
116 Posts
Controller Variable Int operater..
Jul 14, 2012 11:02 PM|LINK
I need to change the category navigation to get childsiblingsonly.
A parent category has a categoryID and a ParentCategoryID of 0. A child category has a categoryID and is mapped to the ParentCategoryID with its own CategoryId as seen in the example below.
There are two methods in the catalog controller a Non Action and Public Action seen below. When I navigate to the product from the child category the navigation view disappears but I would like it to remain on the currentCategory Id the product are connected to if makes sense.
[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; }and the public action
[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); }CPrakash82
All-Star
18722 Points
2900 Posts
Re: Controller Variable Int operater..
Jul 15, 2012 01:16 PM|LINK
I guess it is hard to understand what you have implemeneted here, but suggest to use two partial view and refer them that way your one view will be constant and other will keep changing.
If you can post specific question, it will help.
Thanks,
wertyuio1
Member
67 Points
116 Posts
Re: Controller Variable Int operater..
Jul 15, 2012 01:41 PM|LINK
Hi Thanks for your reply.
The partial view is as follow's
@model IList<CategoryNavigationModel> @using Nop.Core.Domain.Catalog @using Nop.Core.Infrastructure @using Nop.Services.Catalog @using Nop.Web.Models.Catalog @{ var categoryPadding = 15; } @if (Model.Count > 0) { <div class="block block-category-navigation"> <div class="title"> @T("Categories") </div> <div class="clear"> </div> <div class="listbox"> <ul> @foreach (var category in Model) { <li class="@(category.IsActive ? "active" : "inactive")" @if (category.NumberOfParentCategories > 0) { if (this.ShouldUseRtlTheme()) { <text>style="margin-right: @(category.NumberOfParentCategories * categoryPadding)px"</text> } else { <text>style="margin-left: @(category.NumberOfParentCategories * categoryPadding)px"</text> } } ><a href="@Url.RouteUrl("Category", new { categoryId = category.Id, SeName = category.SeName })">@category.Name @if (category.DisplayNumberOfProducts) { <text> (@(category.NumberOfProducts))</text> } </a></li> } </ul> </div> </div>The main question is the variable operand operator. In the non action method. This code below is the area that needs cleaning I think.
var result = new List<CategoryNavigationModel>(); int Id = 0; if (currentCategory != null) Id = currentCategory.Id;On passing the final Category.Id into the Product the partial view dissappears but I need it to remain. My question how do I keep it there with the right variable?
If I change the above code to
var result = new List<CategoryNavigationModel>(); int Id = 0; if (currentCategory != null) Id = 1;All parentcategories direct themselves to categoryId of one - what I need HELP with is keeping the subcategories view in place when the products are reached.
I hope this gives a better understanding.
Thanks
Richard