//========================================================== //========================================================== // Вывод всех категорий в виде дерева ссылок public ActionResult Index() { List <ShopCategory> parentCategories = db.ShopCategories.Where(p => p.ParentId == null).OrderBy(p => p.Name).ToList(); List <ViewCategoryIndex> hierarchy = new List <ViewCategoryIndex>(); foreach (ShopCategory category in parentCategories) { ViewCategoryIndex categoryToList = new ViewCategoryIndex { Id = category.Id, Name = category.Name, Alias = category.Alias }; hierarchy.Add(categoryToList); hierarchy.AddRange(FindChildCategories(category, "")); } return(View(hierarchy)); }
//========================================================== //========================================================== // вспомогательный метод // поиск дочерних категорий и сохранение их в лист для отображения в Index private List <ViewCategoryIndex> FindChildCategories(ShopCategory category, string childtab) { childtab = childtab + "-"; List <ShopCategory> childCategories = db.ShopCategories .Where(c => c.ParentId == category.Id).OrderBy(c => c.Name).ToList(); List <ViewCategoryIndex> childList = new List <ViewCategoryIndex>(); foreach (ShopCategory childCategory in childCategories) { ViewCategoryIndex categoryToLost = new ViewCategoryIndex { Id = childCategory.Id, Name = childtab + " " + childCategory.Name, Alias = childCategory.Alias, Parent = category.Name }; childList.Add(categoryToLost); childList.AddRange(FindChildCategories(childCategory, childtab)); } return(childList); }
//========================================================== //========================================================== public ActionResult Index() { ShopFilter filter = new ShopFilter(); // список вкусов List <ShopProductsTaste> tastes = db.ShopProductsTastes.OrderBy(c => c.Name).ToList(); filter.TasteList = new SelectList(tastes, "Id", "Name"); // список брэндов List <ShopProductsBrand> brands = db.ShopProductsBrands.OrderBy(c => c.Name).ToList(); filter.BrandList = new SelectList(brands, "Id", "Name"); // категории List <ShopCategory> parentCategories = db.ShopCategories.Where(p => p.ParentId == null).OrderBy(p => p.Name).ToList(); List <ViewCategoryIndex> hierarchy = new List <ViewCategoryIndex>(); foreach (ShopCategory category in parentCategories) { ViewCategoryIndex categoryToList = new ViewCategoryIndex { Id = category.Id, Name = category.Name, Alias = category.Alias }; hierarchy.Add(categoryToList); hierarchy.AddRange(FindChildCategories(category, "")); } filter.CategoriesList = new SelectList(hierarchy, "Id", "Name"); // сортировка List <ShopSorting> sortingList = new List <ShopSorting>(); ShopSorting sort = new ShopSorting { Id = 1, Name = "Возростание цены" }; sortingList.Add(sort); sort = new ShopSorting { Id = 2, Name = "Убывание цены" }; sortingList.Add(sort); sort = new ShopSorting { Id = 3, Name = "По алфавиту А-Я" }; sortingList.Add(sort); sort = new ShopSorting { Id = 4, Name = "По алфавиту Я-А" }; sortingList.Add(sort); sort = new ShopSorting { Id = 5, Name = "Новые вначале" }; sortingList.Add(sort); sort = new ShopSorting { Id = 6, Name = "Новые в конце" }; sortingList.Add(sort); filter.SortingList = new SelectList(sortingList, "Id", "Name"); return(View(filter)); }