Example #1
0
        public ActionResult List([DataSourceRequest] DataSourceRequest request, string keyword = null, int categoryId = 0)
        {
            int total = 0;
            SortDescriptor sortDescriptor = (request.Sorts != null && request.Sorts.Count > 0) ? request.Sorts.FirstOrDefault() : new SortDescriptor("DisplayOrder", ListSortDirection.Ascending);

            sortDescriptor.Member = sortDescriptor.Member ?? "DisplayOrder";
            Func<IQueryable<Product>, IOrderedQueryable<Product>> order;
            Expression<Func<Product, bool>> filter = x => x.IsActive == true;

            if (!string.IsNullOrEmpty(keyword))
            {
                filter = x => x.Name.Contains(keyword);
            }

            if (categoryId > 0)
            {
                filter = x => x.CategoryId.Equals(categoryId);
            }

            var data = new List<Product>();
            switch (sortDescriptor.Member)
            {
                case "CategoryId":
                    if (sortDescriptor.SortDirection == ListSortDirection.Ascending)
                    {
                        order = x => x.OrderBy(y => y.CategoryId).ThenBy(z => z.Name);
                    }
                    else
                    {
                        order = x => x.OrderByDescending(y => y.CategoryId).ThenBy(z => z.Name);
                    }
                    break;
                case "Name":
                    if (sortDescriptor.SortDirection == ListSortDirection.Ascending)
                    {
                        order = x => x.OrderBy(y => y.Name);
                    }
                    else
                    {
                        order = x => x.OrderByDescending(y => y.Name);
                    }
                    break;
                case "CreatedDate":
                    if (sortDescriptor.SortDirection == ListSortDirection.Ascending)
                    {
                        order = x => x.OrderBy(y => y.CreatedDate);
                    }
                    else
                    {
                        order = x => x.OrderByDescending(y => y.CreatedDate);
                    }
                    break;
                default:
                    if (sortDescriptor.SortDirection == ListSortDirection.Ascending)
                    {
                        order = x => x.OrderBy(y => y.DisplayOrder).ThenBy(z => z.Name);
                    }
                    else
                    {
                        order = x => x.OrderByDescending(y => y.DisplayOrder).ThenBy(z => z.Name);
                    }
                    break;
            }

            data = _productService.Select(filter, order, null, request.Page, request.PageSize).ToList();

            var products = data.ToListModel();

            foreach (var product in products)
            {
                product.TotalGallery = _galleryService.GetListGalleryDetailByObjectId(product.Id, (int)GalleryCategory.Product, (int)GalleryType.ImageAndVideo).Count;
            }

            total = _productService.Select(filter, order, null, null, null).Count();
            ViewBag.Total = total;
            var result = new DataSourceResult()
            {
                Data = products,
                Total = total
            };
            return Json(result);
        }
Example #2
0
        public ActionResult List([DataSourceRequest] DataSourceRequest request)
        {
            int _total = 0;
            SortDescriptor sortDescriptor = (request.Sorts != null && request.Sorts.Count > 0) ? request.Sorts.FirstOrDefault() : new SortDescriptor("Id", ListSortDirection.Ascending);

            sortDescriptor.Member = sortDescriptor.Member ?? "Title";
            Func<IQueryable<Banner>, IOrderedQueryable<Banner>> order;

            var data = new List<Banner>();
            switch (sortDescriptor.Member)
            {
                case "Title":
                    if (sortDescriptor.SortDirection == ListSortDirection.Ascending)
                    {
                        order = x => x.OrderBy(y => y.Title);
                    }
                    else
                    {
                        order = x => x.OrderByDescending(y => y.Title);
                    }
                    break;
                case "DisplayOrder":
                    if (sortDescriptor.SortDirection == ListSortDirection.Ascending)
                    {
                        order = x => x.OrderBy(y => y.DisplayOrder).ThenBy(z => z.Title);
                    }
                    else
                    {
                        order = x => x.OrderByDescending(y => y.DisplayOrder).ThenBy(z => z.Title);
                    }
                    break;
                default:
                    if (sortDescriptor.SortDirection == ListSortDirection.Ascending)
                    {
                        order = x => x.OrderBy(y => y.Id);
                    }
                    else
                    {
                        order = x => x.OrderByDescending(y => y.Id);
                    }
                    break;
            }

            data = _bannerService.Select(null, order, null, request.Page, request.PageSize).ToList();

            var banners = data.ToListModel();

            _total = _bannerService.Select(null, order, null, null, null).Count();
            ViewBag.Total = _total;
            var result = new DataSourceResult()
            {
                Data = banners,
                Total = _total
            };
            return Json(result);
        }
        public ActionResult List([DataSourceRequest] DataSourceRequest request, string keyword = null)
        {
            int total = 0;
            SortDescriptor sortDescriptor = (request.Sorts != null && request.Sorts.Count > 0) ? request.Sorts.FirstOrDefault() : new SortDescriptor("DisplayOrder", ListSortDirection.Ascending);

            sortDescriptor.Member = sortDescriptor.Member ?? "DisplayOrder";
            Func<IQueryable<Category>, IOrderedQueryable<Category>> order;
            Expression<Func<Category, bool>> filter = x => x.Id > 0;

            //Expression<Func<YourEntity, bool>> FilterByNameLength(int length) { return x => x.Name.Length > length;}
            if (!string.IsNullOrEmpty(keyword))
            {
                filter = x => x.Name.Contains(keyword);
            }
            var data = new List<Category>();
            switch (sortDescriptor.Member)
            {
                case "Name":
                    if (sortDescriptor.SortDirection == ListSortDirection.Ascending)
                    {
                        order = x => x.OrderBy(y => y.Name);
                    }
                    else
                    {
                        order = x => x.OrderByDescending(y => y.Name);
                    }
                    break;
                default:
                    if (sortDescriptor.SortDirection == ListSortDirection.Ascending)
                    {
                        order = x => x.OrderBy(y => y.DisplayOrder).ThenBy(z => z.Name);
                    }
                    else
                    {
                        order = x => x.OrderByDescending(y => y.DisplayOrder).ThenBy(z => z.Name);
                    }
                    break;
            }

            data = _categoryService.Select(filter, order, null, request.Page, request.PageSize).ToList();

            var categories = data.ToListModel();

            foreach (var category in categories)
            {
                category.TotalGallery = _galleryService.GetListGalleryDetailByObjectId(category.Id, (int)GalleryCategory.Category, (int)GalleryType.ImageAndVideo).Count;
                category.TotalPrint = _galleryService.GetListGalleryDetailByObjectId(category.Id, (int)GalleryCategory.Category, (int)GalleryType.PrintCapbility).Count;
            }

            total = _categoryService.Select(filter, order, null, null, null).Count();
            ViewBag.Total = total;
            var result = new DataSourceResult()
            {
                Data = categories,
                Total = total
            };
            return Json(result);
        }