我在网站项目中实施pagedlist mvc时遇到问题。我用pagedlist mvc来显示局部视图。单击按钮之前,参数不会完成传递,只传递通过的页码,另一个为空。这是我的控制器
public ActionResult StoreItemView(string jenis, string sorting_key, int? Page_No)
对于上一个按钮,它将创建此类链接
localhost:20208/StoreItem/StoreItemView?Page_No=1
与下一个按钮不同,它创建包含所有参数的链接
localhost:20208/StoreItem/StoreItemView?jenis=&sorting_key=&Page_No=2
为什么对上一个和下一个按钮的不同呼叫?
我在cshtml中创建这样的寻呼机
<div id="myPager">
@Html.PagedListPager(
Model,
page => Url.Action(
"StoreItemView",
new
{
jenis = ViewBag.jenis,
sorting_key = ViewBag.sorting_key,
Page_No = page
}
),
PagedListRenderOptions.PageNumbersOnly
)
</div>
我也使用javascript加载部分视图,我的javascript是
<script>
$(function () {
$('#myPager').on('click', 'a', function () {
$.ajax({
url: this.href,
type: 'GET',
cache: false,
success: function (result) {
$('#container_item_store').html(result);
alert("sukses");
},
error: alert("bangsat")
});
return false;
});
});
</script>
我差不多2天就遇到了这个问题。我希望在座的人可以帮助我。谢谢你:)
---- EDIT ------
@model PagedList.IPagedList<MVC_EDOLPUZ.Models.StoreItemModel>
@using System.Globalization
@using PagedList.Mvc
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
<h3><span class="label label-primary">DOLANAN PUZZLE ITEM</span></h3>
<select id="Sorting_Order" name="Sorting" onchange="reloadPartialDDL()">
<option value="0">-Urutkan Berdasarkan-</option>
<option value="nama">Nama</option>
<option value="rendah">Harga Terendah</option>
<option value="tinggi">Harga Tertinggi</option>
</select>
<div id="products" class="row list-group">
@foreach (var item in Model)
{
<div class="item col-xs-5 col-lg-3">
<div class="thumbnail">
<img class="group list-group-image img-responsive" src="@Url.Content(@item.gambar_barang)" alt="" />
<div class="caption">
<h4 class="group inner list-group-item-heading">
@item.nama_barang
</h4>
<p class="group inner list-group-item-text">
<span class="label label-warning">@item.deksripsi_barang</span>
</p>
<div class="row">
<div class="col-xs-1 col-md-6">
<input id="@item.nama_barang" type="number" class="rating" min="1" max="5" step="0.5" data-size="xs" value="@item.rating_barang">
</div>
<script>
$('#@item.nama_barang').rating('refresh', { disabled: true, showClear: false, showCaption: false });
</script>
</div>
<div class="row">
<div class="col-lg-5 col-xs-4">
<p class="lead" style="font-weight: bolder; color: red;">
@string.Format(new CultureInfo("id-ID"), "{0:C}", @item.harga_barang)
</p>
</div>
<div class="col-lg-1 col-xs-2">
<a class="btn btn-success btn-responsive btn-xs" onclick="addItemToCart('@item.id_barang')" href="#">Add to cart</a>
</div>
</div>
</div>
</div>
</div>
}
</div>
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@*@Html.PagedListPager(Model, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))*@
<div id="myPager">
@Html.PagedListPager(
Model,
page => Url.Action(
"StoreItemView",
new
{
jenis = ViewBag.jenis,
sorting_key = ViewBag.sorting_key,
Page_No = page
}
),
PagedListRenderOptions.PageNumbersOnly
)
</div>
<script>
$(function () {
$('#myPager').on('click', 'a', function () {
$.ajax({
url: this.href,
type: 'GET',
cache: false,
success: function (result) {
$('#container_item_store').html(result);
alert("sukses");
},
error: alert("bangsat")
});
return false;
});
});
</script>
这是我的视图代码,以及处理它的控制器
[HttpGet]
public ActionResult StoreItemView(string jenis, string sorting_key, int? Page_No)
{
ViewBag.jenis = jenis;
ViewBag.sorting_key = sorting_key;
List<StoreItemModel> products = StoreItemRepository.getItemList(jenis, sorting_key);
foreach (var items in products)
{
items.rating_barang = StoreItemRepository.getRatingBarang(items.id_barang);
}
int Size_Of_Page = 4;
int No_Of_Page = (Page_No ?? 1);
PagedList.PagedList<StoreItemModel> show = new PagedList.PagedList<StoreItemModel>(products, No_Of_Page, Size_Of_Page);
return PartialView("_StoreItem", show);
}
答案 0 :(得分:0)
请使用如下
<div id="myPager" location="Url.Action("StoreItemView", new {jenis = ViewBag.jenis, sorting_key = ViewBag.sorting_key, Page_No = page})">
@Html.PagedListPager(
Model,
page => Url.Action("StoreItemView"),
PagedListRenderOptions.PageNumbersOnly
)
和javascript必须一样 在使用之前,请检查 ViewBag.sorting_key , ViewBag.jenis 在javascript中使用alert保留任何值。并且我无法看到任何带有id =&#34; container_item_store&#34;的标签。确保 container_item_store ID必须放在视图中的某些位置。
<script>
$(function () {
$('#myPager').on('click', 'a', function () {
var location = $(this).attr('location');
$.ajax({
url: location,
type: 'GET',
cache: false,
success: function (result) {
$('#container_item_store').html(result);
alert("sukses");
},
error: alert("bangsat")
});
return false;
});
});