首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pagedlist in partialview

Pagedlist in partialview
EN

Stack Overflow用户
提问于 2020-05-30 18:28:18
回答 1查看 35关注 0票数 0

我将PagedList移到了PartialView,因为我在我的页面中使用了价格过滤器,当我点击页面编号2时,页面中的元素数量是正确的,但所有页面都被刷新了,我只想刷新我的部分视图内容。如何做到这一点?

PartialView

代码语言:javascript
复制
@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>

主视图使用javascript调用部分

代码语言: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");
            });
        }
代码语言:javascript
复制
<div class="col-md-11" id="tableContainer">
            @{
                var filterProdViewModel = new FilterProductsViewModel();
                filterProdViewModel.Products = Model.Products;
                Html.RenderPartial("FilterProducts", filterProdViewModel);
             }
</div>

控制器

代码语言:javascript
复制
 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);
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-31 17:07:58

我发现了这个问题,我必须把pagenumber和pagesize参数放到viewmodel中,并把它发送到我的javascript中的partialview来解决这个问题。

代码语言:javascript
复制
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");
            });
        }
    ```
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62101116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档