我在表中显示数据并进行一些分页和排序。我有一个复选框。 当我从一个页面移动到另一个页面时,如果选中它们,我将保持选中复选框。 当我在页面之间导航时,我不希望取消选中复选框。我如何实现这一目标?
这是我的jquery。
<script type="text/javascript">
$(document).ready(function () {
$(".header").click(function (evt) {
var sortfield = $(evt.target).data("sortfield");
if ($("#SortField").val() == sortfield)
{
if($("#SortDirection").val()=="ascending")
{
$("#SortDirection").val("descending");
}
else
{
$("#SortDirection").val("ascending");
}
}
else
{
$("#SortField").val(sortfield);
$("#SortDirection").val("ascending");
}
evt.preventDefault();
$("form").submit();
});
$(".pager").click(function (evt) {
var pageindex = $(evt.target).data("pageindex");
$("#CurrentPageIndex").val(pageindex);
evt.preventDefault();
$("form").submit();
});
});
</script>
Here is my html page
<body>
<h1>List of Customers</h1>
@{
PageSortOnlineEx.Models.SortingPagingInfo info = ViewBag.SortingPagingInfo;
}
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.Hidden("SortField", info.SortField)
@Html.Hidden("SortDirection", info.SortDirection)
@Html.Hidden("PageCount", info.PageCount)
@Html.Hidden("PageSize", info.PageSize)
@Html.Hidden("CurrentPageIndex", info.CurrentPageIndex)
<table border="1" cellpadding="10">
<tr>
<th><a href="#" data-sortfield="">Select</a></th>
<th><a href="#" data-sortfield="CustomerID" class="header">CustomerID</a></th>
<th><a href="#" data-sortfield="CompanyName" class="header">CompanyName</a></th>
<th><a href="#" data-sortfield="ContactName" class="header">ContactName</a></th>
<th><a href="#" data-sortfield="Country" class="header">Country</a></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@Html.CheckBox("select", false)</td>
<td>@item.CustomerID</td>
<td>@item.CompanyName</td>
<td>@item.ContactName</td>
<td>@item.Country</td>
</tr>
}
<tr>
<td colspan="4">
@for (var i = 0; i < info.PageCount; i++)
{
if (i == info.CurrentPageIndex)
{
<span>@(i + 1)</span>
}
else
{
<a href="#" data-pageindex="@i" class="pager">@(i + 1)</a>
}
}
</td>
</tr>
</table>
}
</body>
这是我的控制器代码
public ActionResult Index()
{
using (NorthwindEntities db = new NorthwindEntities())
{
SortingPagingInfo info = new SortingPagingInfo();
info.SortField = "CustomerID";
info.SortDirection = "ascending";
info.PageSize = 10;
info.PageCount = Convert.ToInt32(Math.Ceiling((double)(db.Customers.Count() / info.PageSize)));
info.CurrentPageIndex = 0;
var query = db.Customers.OrderBy(c => c.CustomerID).Take(info.PageSize);
ViewBag.SortingPagingInfo = info;
List<Customer> model = query.ToList();
return View(model);
}
}
[HttpPost]
public ActionResult Index(SortingPagingInfo info)
{
using (NorthwindEntities db = new NorthwindEntities())
{
IQueryable<Customer> query = null;
switch (info.SortField)
{
case "CustomerID":
query = (info.SortDirection == "ascending" ? db.Customers.OrderBy(c => c.CustomerID) : db.Customers.OrderByDescending(c => c.CustomerID));
break;
case "CompanyName":
query = (info.SortDirection == "ascending" ? db.Customers.OrderBy(c => c.CompanyName) : db.Customers.OrderByDescending(c => c.CompanyName));
break;
case "ContactName":
query = (info.SortDirection == "ascending" ? db.Customers.OrderBy(c => c.ContactName) : db.Customers.OrderByDescending(c => c.ContactName));
break;
case "Country":
query = (info.SortDirection == "ascending" ? db.Customers.OrderBy(c => c.Country) : db.Customers.OrderByDescending(c => c.Country));
break;
}
query = query.Skip(info.CurrentPageIndex * info.PageSize).Take(info.PageSize);
ViewBag.SortingPagingInfo = info;
List<Customer> model = query.ToList();
return View(model);
}
}