コード例 #1
0
        public IActionResult Index(int id, string title, int page)
        {
            var query = db.Article.Where(x => x.Status == true);

            string url = "/tintuc/p{0}";

            //sắp xếp theo ngày đăng
            var data = query.OrderByDescending(x => x.CreateTime);

            // dem so luong du lieu  phu hop
            int totals = data.Count();

            // sử lý phân trang
            if (page <= 0)
            {
                page = 1;
            }
            int pageSize = 3;
            int skip     = (page - 1) * pageSize;

            var listData = data.Skip(skip).Take(pageSize);

            // TAO DŨ LIEU CHO VIEW
            ListDatasource datasource = new ListDatasource();

            datasource.Total    = totals;
            datasource.Page     = page;
            datasource.PageSize = pageSize;
            datasource.MaxPage  = 6;
            datasource.Url      = url;
            datasource.Data     = listData.ToList();

            return(View(datasource));
        }
コード例 #2
0
        public IActionResult Index̣̣(int mid, int cid, string title, int page = 1)
        {
            var query = db.Product
                        .Where(x => x.Status == true);

            string url = "/san-pham/p{0}";

            //nếu có mid thì load sản phẩm theo dk mid
            if (mid > 0)
            {
                url   = $"/san-pham/{mid}/{title}/p{{0}}"; //p{{0}} : là do page này mình k lấy page hiện tại nên mình k thể bỏ vào biến p{page} dc mà phải để là p{0} nhưng biến $ sẽ hiểu đây chỉ là tên biến k hiểu nên phải xài theo kiểu sql là dấu nào k dc thì mình chơi 2 dấu :)) (cách tà đạo)
                query = query.Where(x => x.ProductCategory.ProductMainCategoryID == mid);
            }

            //nếu có cid thì load sản phẩm theo dk cid
            if (cid > 0)
            {
                url   = $"/san-pham/{mid}/{cid}/{title}/p{{0}}";
                query = query.Where(x => x.ProductCategoryID == cid);
            }


            //sắp xếp mới nhất
            var data = query.OrderByDescending(x => x.CreateTime);

            //đếm số lượng dữ liệu phù hợp
            int totals = data.Count();

            // xử lý phân trang
            if (page <= 0)
            {
                page = 1;
            }

            int pageSize = 12; // đây là số phần tử xuất hiện
            int skip     = (page - 1) * pageSize;

            var listData = data.Skip(skip).Take(pageSize);

            //tạo dữ liệu cho view
            ListDatasource datasource = new ListDatasource();

            datasource.Totals   = totals;
            datasource.Page     = page;
            datasource.PageSize = pageSize;
            datasource.MaxPage  = 6;
            datasource.Url      = url;
            datasource.Data     = listData.ToList();


            return(View(datasource));
        }
コード例 #3
0
        public IActionResult Index(int mid, int cid, string title, int page)
        {
            var query = db.Product.Include(x => x.ProductCategory)
                        .Where(x => x.Status == true);

            string url = "/sanpham/p{0}";

            // nếu có mid thì lọc theo mid
            if (mid > 0)
            {
                url   = $"/sanpham/{mid}/{title}/p{{0}}"; // do xài $ của mvc 5 nên phần  p{0} là phần page dc hiểu là page{0} nên phải sử dụng p{{0}} giống như sql dấu nào cấm thì chơi 2 dấu cùng 1 lúc :))
                query = query.Where(x => x.ProductCategory.ProductMainCategoryID == mid);
            }

            // nếu có cid thì lọc theo cid
            if (cid > 0)
            {
                url   = $"/sanpham/{mid}/{cid}/{title}/p{{0}}";
                query = query.Where(x => x.ProductCategoryID == cid);
            }

            //sắp xếp theo ngày đăng
            var data = query.OrderByDescending(x => x.CreateTime);

            // dem so luong du lieu  phu hop
            int totals = data.Count();

            // sử lý phân trang
            if (page <= 0)
            {
                page = 1;
            }
            int pageSize = 9;
            int skip     = (page - 1) * pageSize;

            var listData = data.Skip(skip).Take(pageSize);

            // TAO DŨ LIEU CHO VIEW
            ListDatasource datasource = new ListDatasource();

            datasource.Total    = totals;
            datasource.Page     = page;
            datasource.PageSize = pageSize;
            datasource.MaxPage  = 6;
            datasource.Url      = url;
            datasource.Data     = listData.ToList();

            return(View(datasource));
        }
コード例 #4
0
        public IActionResult Index̣̣(int page)
        {
            var data = db.Article.Where(x => x.Status == true);

            // đường dẫn URL mặc định dùng để phần trang
            string url = "/tin-tuc/p{0}";

            //sắp xếp bài mới nhất
            data = data.OrderByDescending(x => x.CreateTime);

            //đếm số lượng dữ liệu phù hợp
            int totals = data.Count();

            // xử lý phân trang
            if (page <= 0)
            {
                page = 1;
            }

            int pageSize = 7; // đây là số phần tử xuất hiện
            int skip     = (page - 1) * pageSize;

            var listData = data.Skip(skip).Take(pageSize);

            //tạo dữ liệu cho view
            ListDatasource datasource = new ListDatasource();

            datasource.Totals   = totals;
            datasource.Page     = page;
            datasource.PageSize = pageSize;
            datasource.MaxPage  = 6;
            datasource.Url      = url;
            datasource.Data     = listData.ToList();


            return(View(datasource));
        }
コード例 #5
0
        public async Task <IActionResult> CategoryDetail(string tenloai, string chatlieu, string loaisanpham, string tamgia, int page)
        {
            var tenloaisanpham = await _categoryRepository.All.FirstOrDefaultAsync(x => x.Slug == (tenloai));

            var tenloaisanphamchitiet = tenloaisanpham.CategoryName;

            ViewBag.tenloai = tenloai;
            var listmaterial = await _materialRepository.All.ToListAsync();

            var listproducttype = await _productTypeRepository.All.ToListAsync();

            ViewBag.Dschatlieu = listmaterial;
            ViewBag.DsLoai     = listproducttype;
            if (_cache.TryGetValue("CACHE_MASTER_PRODUCT", out List <ProductViewModel> cLstProd))
            {
                var    listactive = cLstProd.Where(x => x.Actived == true && x.CategoryName == tenloaisanphamchitiet).ToList();
                string url        = $"/loaisanpham/{tenloai}/p{{0}}";
                if (loaisanpham == null && chatlieu == null)
                {
                    listactive = cLstProd.Where(x => x.Actived == true && x.CategoryName == tenloaisanphamchitiet).ToList();
                }
                if (tamgia != null && loaisanpham == null && chatlieu == null)
                {
                    ViewBag.chatlieu    = null;
                    ViewBag.loaisanpham = null;
                    ViewBag.tamgia      = tamgia;
                    url = $"/loaisanpham/{tenloai}/{tamgia}/p{{0}}";
                    if (tamgia.Contains("be20"))
                    {
                        listactive = listactive.Where(x =>
                                                      x.PriceAfter <= double.Parse("20000000")).ToList();
                    }
                    else if (tamgia.Contains("lon20"))
                    {
                        listactive = listactive.Where(x =>
                                                      x.PriceAfter > double.Parse("20000000") &&
                                                      x.PriceAfter <= double.Parse("150000000")).ToList();
                    }
                    else if (tamgia.Contains("full"))
                    {
                        listactive = listactive.Where(x =>
                                                      x.PriceAfter > double.Parse("150000000")).ToList();
                    }
                }
                if (chatlieu != null && loaisanpham != null)
                {
                    ViewBag.chatlieu    = chatlieu;
                    ViewBag.loaisanpham = loaisanpham;
                    url = $"/loaisanpham/{tenloai}/{chatlieu}/{loaisanpham}/p{{0}}";
                    var materialname    = listmaterial.FirstOrDefault(x => x.SeoAlias == chatlieu)?.MaterialName;
                    var producttypename = listproducttype.FirstOrDefault(x => x.SeoAlias == loaisanpham)?.TypeName;
                    listactive = listactive.Where(x =>
                                                  x.MaterialName.Equals(materialname) &&
                                                  x.ProductTypeName.Equals(producttypename)).ToList();
                }
                // nếu có mid thì lọc theo mid
                if (chatlieu != null && loaisanpham == null)
                {
                    ViewBag.chatlieu = chatlieu;
                    var materialname = listmaterial.FirstOrDefault(x => x.SeoAlias == chatlieu)?.MaterialName;
                    url        = $"/loaisanpham/{tenloai}/{chatlieu}/p{{0}}";
                    listactive = listactive.Where(x =>
                                                  x.MaterialName.Equals(materialname)).ToList();
                }

                // nếu có cid thì lọc theo cid
                if (loaisanpham != null && chatlieu == null)
                {
                    ViewBag.loaisanpham = loaisanpham;
                    var producttypename = listproducttype.FirstOrDefault(x => x.SeoAlias == loaisanpham)?.TypeName;
                    url        = $"/loaisanpham/{tenloai}/{loaisanpham}/p{{0}}";
                    listactive = listactive.Where(x =>
                                                  x.ProductTypeName.Equals(producttypename)).ToList();
                }



                //sắp xếp theo ngày đăng
                var data = listactive.OrderByDescending(x => x.CreateAt.GetValueOrDefault()).ToList();

                // dem so luong du lieu  phu hop
                int totals = data.Count();

                // sử lý phân trang
                if (page <= 0)
                {
                    page = 1;
                }
                int pageSize = 3;
                int skip     = (page - 1) * pageSize;

                var listData = data.Skip(skip).Take(pageSize);

                // TAO DŨ LIEU CHO VIEW
                ListDatasource datasource = new ListDatasource();
                datasource.Total    = totals;
                datasource.Page     = page;
                datasource.PageSize = pageSize;
                datasource.MaxPage  = 6;
                datasource.Url      = url;
                datasource.Data     = listData.ToList();
                return(await Task.Run(() => View(datasource)));
            }
            else
            {
                MemoryCacheEntryOptions options = new MemoryCacheEntryOptions
                {
                    AbsoluteExpiration = DateTime.Now + TimeSpan.FromHours(3),
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(3),
                    SlidingExpiration = TimeSpan.FromHours(3),
                    Priority          = CacheItemPriority.NeverRemove
                };
                IEnumerable <ProductViewModel> list = _productRepository.GetProductViewModels().ToList();
                foreach (var item in list)
                {
                    item.PriceType = Enum.GetName(typeof(PriceType), int.Parse(item.PriceType));
                }
                _cache.Set("CACHE_MASTER_PRODUCT", list, options);
                var    listactive = list.Where(x => x.Actived == true && x.CategoryName == tenloaisanphamchitiet).ToList();
                string url        = $"/loaisanpham/{tenloai}/p{{0}}";
                if (loaisanpham == null && chatlieu == null)
                {
                    listactive = list.Where(x => x.Actived == true && x.CategoryName == tenloaisanphamchitiet).ToList();
                }
                if (tamgia != null && loaisanpham == null && chatlieu == null)
                {
                    ViewBag.chatlieu    = null;
                    ViewBag.loaisanpham = null;
                    ViewBag.tamgia      = tamgia;
                    url = $"/loaisanpham/{tenloai}/{tamgia}/p{{0}}";
                    if (tamgia.Contains("be20"))
                    {
                        listactive = listactive.Where(x =>
                                                      x.PriceAfter <= double.Parse("20000000")).ToList();
                    }
                    else if (tamgia.Contains("lon20"))
                    {
                        listactive = listactive.Where(x =>
                                                      x.PriceAfter > double.Parse("20000000") &&
                                                      x.PriceAfter <= double.Parse("150000000")).ToList();
                    }
                    else if (tamgia.Contains("full"))
                    {
                        listactive = listactive.Where(x =>
                                                      x.PriceAfter > double.Parse("150000000")).ToList();
                    }
                }
                if (chatlieu != null && loaisanpham != null)
                {
                    url        = $"/loaisanpham/{tenloai}/{chatlieu}/{loaisanpham}/p{{0}}";
                    listactive = listactive.Where(x =>
                                                  x.MaterialName.ToLower().Equals(chatlieu.ToLower()) &&
                                                  x.ProductTypeName.ToLower().Equals(loaisanpham.ToLower())).ToList();
                }
                // nếu có mid thì lọc theo mid
                if (chatlieu != null && loaisanpham == null)
                {
                    url        = $"/loaisanpham/{tenloai}/{chatlieu}/p{{0}}";
                    listactive = listactive.Where(x =>
                                                  x.MaterialName.ToLower().Equals(chatlieu.ToLower())).ToList();
                }

                // nếu có cid thì lọc theo cid
                if (loaisanpham != null && chatlieu == null)
                {
                    url        = $"/loaisanpham/{tenloai}/{loaisanpham}/p{{0}}";
                    listactive = listactive.Where(x =>
                                                  x.ProductTypeName.ToLower().Equals(loaisanpham.ToLower())).ToList();
                }

                //sắp xếp theo ngày đăng
                var data = listactive.OrderByDescending(x => x.CreateAt.GetValueOrDefault()).ToList();

                // dem so luong du lieu  phu hop
                int totals = data.Count;

                // sử lý phân trang
                if (page <= 0)
                {
                    page = 1;
                }
                int pageSize = 3;
                int skip     = (page - 1) * pageSize;

                var listData = data.Skip(skip).Take(pageSize);

                // TAO DŨ LIEU CHO VIEW
                ListDatasource datasource = new ListDatasource();
                datasource.Total    = totals;
                datasource.Page     = page;
                datasource.PageSize = pageSize;
                datasource.MaxPage  = 6;
                datasource.Url      = url;
                datasource.Data     = listData.ToList();
                return(await Task.Run(() => View(datasource)));
            }
        }