Ejemplo n.º 1
0
        // GET: Categories
        //[Authorize(Roles = "admin")]
        //[Authorize(Roles = "manager")]
        public ActionResult Index(string sortOrder, string searchString)
        {
            CategoryViewModel vm = new CategoryViewModel();
            //Find all cat by language
            int lang        = LanguageBL.CheckLanguageSession();
            var categoriesT = db.SPP_CategoryTransDistinct(lang).ToList();
            //Find relation for all cat
            IList <int> parentOnly         = db.SPP_ParentCategoriesSubTransDistinct(lang).Select(p => p.idCategory).ToList();
            IList <int> childOnly          = db.SPP_ChildCategoriesTransDistinct(lang).Select(p => p.idCategory).ToList();
            IList <CategoryViewModel> list = new List <CategoryViewModel>();
            string rel;

            foreach (var item in categoriesT)
            {
                if (childOnly.Contains(item.idCategory) && parentOnly.Contains(item.idCategory))
                {
                    rel = "parent/sous-catégorie";
                }
                else if (childOnly.Contains(item.idCategory))
                {
                    rel = "sous-catégorie";
                }
                else
                {
                    rel = "catégorie parent";
                }

                CategoryViewModel cat = new CategoryViewModel
                {
                    IdCat    = item.idCategory,
                    NameCat  = item.nameCategory,
                    Image    = item.imageCat,
                    Relation = rel
                };
                list.Add(cat);
            }

            ViewBag.NameSortParam = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            if (!String.IsNullOrEmpty((searchString)))
            {
                list = list.Where(s => s.NameCat.ToLower().StartsWith(searchString.ToLower())).ToList();
            }
            switch (sortOrder)
            {
            case "name_desc":
                list = list.OrderByDescending(d => d.NameCat).ToList();
                break;

            default:
                list = list.OrderBy(d => d.NameCat).ToList();
                break;
            }

            return(View(list));
        }