我将PagedList移到了PartialView,因为我在我的页面中使用了价格过滤器,当我点击页面编号2时,页面中的元素数量是正确的,但所有页面都被刷新了,我只想刷新我的部分视图内容。如何做到这一点?
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>主视图使用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>控制器
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);
}发布于 2020-05-31 17:07:58
我发现了这个问题,我必须把pagenumber和pagesize参数放到viewmodel中,并把它发送到我的javascript中的partialview来解决这个问题。
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");
});
}
```https://stackoverflow.com/questions/62101116
复制相似问题