Ejemplo n.º 1
0
        public async Task <IActionResult> Index(int page = 1, int limit = 10, string name = "")
        {
            try
            {
                var queryCategorys = _db.Category.AsNoTracking().OrderByDescending(u => u.Id);
                if (!string.IsNullOrWhiteSpace(name))
                {
                    queryCategorys = queryCategorys.Where(u => u.CategoryName.Contains(name)).OrderByDescending(u => u.SortNo);
                }
                var categoryResult = await PaginatedList <Category> .CreatePaginatedAsync(queryCategorys, page, limit);

                ViewBag.PageIndex       = categoryResult.PageIndex;
                ViewBag.PageTotal       = categoryResult.PageTotal;
                ViewBag.PageCount       = categoryResult.PageCount;
                ViewBag.HasPreViousPage = categoryResult.HasPreViousPage;
                ViewBag.HasNextPage     = categoryResult.HasNextPage;
                if (!string.IsNullOrWhiteSpace(HttpContext.Request.QueryString.ToString()))
                {
                    ViewBag.Query = HttpContext.Request.QueryString.ToString().Substring(0);
                }
                ViewBag.PageUrl        = "/categorym";
                ViewData["SearchName"] = name;
                var categoryMaps = _mapper.Map <List <Category>, List <CategoryDto> >(categoryResult);
                ViewBag.Categorys = new List <CategoryDto>();
                if (categoryMaps.Count > 0)
                {
                    ViewBag.Categorys = categoryMaps;
                }
                if (TempData["ErrorMessage"] != null)
                {
                    ViewData["ErrorMessage"] = TempData["ErrorMessage"];
                }
            }
            catch (Exception ex)
            {
                ViewData["ErrorMessage"] = ex.Message;
            }
            finally
            {
                ViewData["CategoryM"] = "active";
            }
            return(View());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Index(int page = 1, int limit = 10, string title = "")
        {
            try
            {
                var queryPosts = _db.Post.AsNoTracking().OrderByDescending(u => u.Id);
                if (!string.IsNullOrWhiteSpace(title))
                {
                    queryPosts = queryPosts.Where(u => u.Title.Contains(title)).OrderByDescending(u => u.Id);
                }
                var postResult = await PaginatedList <Post> .CreatePaginatedAsync(queryPosts, page, limit);

                ViewBag.PageIndex       = postResult.PageIndex;
                ViewBag.PageTotal       = postResult.PageTotal;
                ViewBag.PageCount       = postResult.PageCount;
                ViewBag.HasPreViousPage = postResult.HasPreViousPage;
                ViewBag.HasNextPage     = postResult.HasNextPage;
                if (!string.IsNullOrWhiteSpace(HttpContext.Request.QueryString.ToString()))
                {
                    ViewBag.Query = HttpContext.Request.QueryString.ToString().Substring(0);
                }
                ViewBag.PageUrl         = "/postm";
                ViewData["SearchTitle"] = title;
                var postMaps = _mapper.Map <List <Post>, List <PostDto> >(postResult);
                ViewBag.Posts = new List <PostDto>();
                if (postMaps.Count > 0)
                {
                    ViewBag.Posts = postMaps;
                }
                if (TempData["ErrorMessage"] != null)
                {
                    ViewData["ErrorMessage"] = TempData["ErrorMessage"];
                }
            }
            catch (Exception ex)
            {
                ViewData["ErrorMessage"] = ex.Message;
            }
            finally
            {
                ViewData["PostM"] = "active";
            }
            return(View());
        }