Answer the question
In order to leave comments, you need to log in
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);
}
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);
}
<div class="container" id="tableBody" >
@Html.Action("Summary")
</div>
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question