M
M
Michael2017-12-04 02:02:03
ASP.NET
Michael, 2017-12-04 02:02:03

How to make pagination work using PartialView() with Ajax.BeginForm()?

Hello! I use Ajax.Beginform() to sort the list of products on my site without reloading the entire page, for this I use 2 methods in the controller, the first one is this one

public ActionResult List(string category, int page = 1)
        {
            ListViewModel model = new ListViewModel
            {
                Fields = new List<SelectListItem>
                {
                   new SelectListItem { Text = "Order By Descending", Value = "OrderByDescending" },
                   new SelectListItem { Text = "Order By Ascending", Value = "OrderByAscending" },
                },
                Furnitures = repository.Furnitures
                .Where(p => category == null || p.Category.Name == category)
                .Skip((page - 1) * pageSize)
                .Take(pageSize)
                .ToList(),
                InfoPages = new InfoPage
                {
                    CurrentPage = page,
                    ItemsPerPage = pageSize,
                    TotalItems = category == null ? repository.Furnitures.Count() :
                    repository.Furnitures.Where(furniture => furniture.Category.Name == category).Count()
                },
                CurrentCategory = category,
               
            };
            return View(model);
        }

This is the method that does pagination and navigation, here is the second one that returns PartialView()
public ActionResult Summary(string SelectedValue)
        {
            ViewBag.CurrentSort = SelectedValue;
            IEnumerable<Furniture> result = repository.Furnitures;
            if (SelectedValue != null)
            {
                if (SelectedValue.Equals("OrderByDescending"))
                {
                    result = repository.Furnitures.OrderBy("Price desc")
                .ToList();


                }
                else if (SelectedValue.Equals("OrderByAscending"))
                {
                    result = repository.Furnitures.OrderBy("Price asc").ToList();
                }
                else
                {
                    result = repository.Furnitures.OrderBy("FurnitureId").ToList();

                }

            }
            return PartialView(result);
        }

I call PartialView in the List view like this
<div class="container" id="tableBody" >                
@Html.Action("Summary")         
</div>

In the partial view itself, I go through the foreach loop through all the products . I
use dropdown for sorting, ajax works well with one exception, now pagination and, accordingly, category navigation do not work, that is, all products are displayed on one page, I understand that the problem is in the logic in the Summary method , as I'm essentially only returning all items from the DB. but how can I hook up pagination with navigation in my partial View method?? I return IEnumerable there, and I ask for your advice or help on how I should proceed. Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eRKa, 2017-12-04
@M-Misha-M

Well, you filter the data only when the page is loaded, but there is no filtering in the Summary, you take all the Furnitures and only sort it, so when you click on another page, it will display everything you have. Try to move the filtering from the List into a separate method and apply it to the result in the Summary as well.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question