Home > Software engineering >  Getting limited data from database using ajax in asp.net core mvc
Getting limited data from database using ajax in asp.net core mvc

Time:11-15

i create a form where i get data from database using loop in asp.net core.i do paginition in that form where i just display 5 items on a page. But Now i have another data which display on that page but i do not want to include into the pagination. So is there any ajax or another solution for it

My view:

@foreach(var item in Model)
                {
                <div >
                    <div >
                        <div >
                            <div >
                                
                                    <img src="@item.pic1" />
                                   
                                <h2>@item.city | <a href="#">Map</a></h2>
                                <hr>
                                <div >
                                    <a href="#">
                                        
                                    </a>
                                    <a href="#">
                                        @item.price price
                                    </a>
                                </div>
                                <hr>
                                <a href="#" >
                                    <svg  height="30px" viewBox="0 0 24 24" fill="#22577e"
                                         xmlns="http://www.w3.org/2000/svg">
                                        <path d="M13.5 8.25L17.25 12M17.25 12L13.5 15.75M17.25 12H6.75" stroke="#22577e"
                                              stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
                                    </svg>
                                </a>
                            </div>

                        </div>
                  }

Controller:

public IActionResult Rent(int PageNumber = 1)
        {

            var data = rdb.GetDataHouse();
           
            ViewBag.Totalpages = Math.Ceiling(data.Count()/5.0);
            data = data.Skip((PageNumber - 1)*5).Take(5).ToList();
            return View(data);
            
        }

CodePudding user response:

You can create a new PaginatedList class to handle pagination:

public class PaginatedList<T> : List<T>
    {
        public int PageIndex { get; private set; }
        public int TotalPages { get; private set; }

        public PaginatedList(List<T> items, int count, int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            TotalPages = (int)Math.Ceiling(count / (double)pageSize);

            this.AddRange(items);
        }

        public bool HasPreviousPage => PageIndex > 1;

        public bool HasNextPage => PageIndex < TotalPages;

        public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int pageIndex, int pageSize)
        {
            var count = await source.CountAsync();
            var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
            return new PaginatedList<T>(items, count, pageIndex, pageSize);
        }
    }

For specific usage and implementation, please refer to enter image description here

  • Related