0

I moved PagedList to PartialView because i use price filter in my page and when click on page number 2 nuber of elements in page are correct but all page was refreshed and i want to refresh only my partialview content. How can do that?

PartialView

@section Scripts{
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
}
<div class="col-md-12">
    Page: @(Model.Products.PageCount < Model.Products.PageNumber ? 0 : Model.Products.PageNumber)/@Model.Products.PageCount

    @Html.PagedListPager(Model.Products, page => Url.Action("FilterProducts",
    new { page, pageSize = ViewBag.psize }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions() { InsertionMode = InsertionMode.Replace, HttpMethod = "GET", UpdateTargetId = "tableContainer" }))
</div>

Main View Calling Partial using javascript

 function GetData(minPrice, maxPrice) {
             $.ajax({
                 url: '@Url.Action("FilterProducts", "Home")',
                 data: {
                     minPrice: minPrice,
                     maxPrice: maxPrice,
                     Category: '@Model.Category',
                     Search: '@Model.Search'
                 }
             })
             .done(function (result) {
                  $('#tableContainer').html(result);
             })
             .fail(function (XMLHttpRequest, textStatus, errorThrown)
            {
                alert("FAIL");
            });
        }
<div class="col-md-11" id="tableContainer">
            @{
                var filterProdViewModel = new FilterProductsViewModel();
                filterProdViewModel.Products = Model.Products;
                Html.RenderPartial("FilterProducts", filterProdViewModel);
             }
</div>

Controller

 public ActionResult Index(string Search, int? page, int? pageSize, int? maxPrice, int? minPrice, string Category = null)
        {

            IPagedList<Product> products;
            List<ProductCategory> prodCat = productCategories.Collection().ToList();
            if (Category == "")
                Category = null;
            int pagesize = (pageSize ?? 5);
            ViewBag.psize = pagesize;
            ViewBag.Min = prodScont.GetMinPrice();
            ViewBag.Max = prodScont.GetMaxPrice();

            products = prodScont.SearchProducts(minPrice, maxPrice, Category, Search).ToPagedList(page ?? 1, pagesize);

            ProductListViewModel model = new ProductListViewModel();
            model.Products = products;
            model.ProductCategories = prodCat;
            model.Category = Category;
            model.Search = Search;
            model.Page = page;
            return View(model);
        }

        public ActionResult FilterProducts(int? page, int? pageSize, int? maxPrice, int? minPrice, string Category = null, string Search = null)
        {
            FilterProductsViewModel model = new FilterProductsViewModel();
            IPagedList<Product> products;
            if (Category == "")
               Category = null;
            int pagesize = (pageSize ?? 5);
            ViewBag.psize = pagesize;

            products = prodScont.SearchProducts(minPrice, maxPrice, Category, Search).ToPagedList(page ?? 1, pagesize);

            model.Products = products;
            return PartialView(model);
        }
  • Does this answer your question? [Ajax Pagination in PagedList.MVC using partial Page](https://stackoverflow.com/questions/17336165/ajax-pagination-in-pagedlist-mvc-using-partial-page) – Jon Ryan May 30 '20 at 10:59
  • `@Html.PagedListPager(Model.Products, page => Url.Action("FilterProducts", new { page, pageSize = ViewBag.psize }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions() { InsertionMode = InsertionMode.Replace, HttpMethod = "GET", UpdateTargetId = "tableContainer" }))` I tried this in my code above but the result is the same – user12805845 May 31 '20 at 07:55
  • When i redirect to Main page in PagedListPager everything with my view is ok but when i change the page nothing happened , when i redirect to partialview page pagination working but refresh all page and vizualize only partial view – user12805845 May 31 '20 at 08:38

1 Answers1

0

I found the problem i had to put pagenumber and pagesize paramethers to viewmodel and send it to partialview in my javascript to fix the problem

function GetData(minPrice, maxPrice) {
             $.ajax({
                 url: '@Url.Action("FilterProducts", "Home")',
                 data: {
                     page: '@Model.Page',
                     pagesize: '@Model.PageSize',
                     minPrice: minPrice,
                     maxPrice: maxPrice,
                     Category: '@Model.Category',
                     Search: '@Model.Search'
                 }
             })
             .done(function (result) {
                  $('#tableContainer').html(result);
             })
             .fail(function (XMLHttpRequest, textStatus, errorThrown)
            {
                alert("FAIL");
            });
        }
    ```