我有一个使用PagedList的页面,但也包含一个用于排序的下拉列表。问题是,每当有人使用下拉列表项进行排序时,每当他们点击“下一个”或“后退”时,下拉列表项都会恢复为默认值,程序通过控制器运行默认(基本上是空白)排序标准,当您导航到第二页或第二页(并返回)时,您会丢失已排序的项目。
这是用于此页面的控制器:
public ActionResult Index(FormCollection dropDownSelection, string currentFilter, int? page)
{
//security
if (!Security.IsViewer(User)) return RedirectToAction("Message", "Home", new { id = 2 });
if (ViewBag.Level == 0) return RedirectToAction("Message", "Home", new { id = 2 });
if (!(Request.HttpMethod == "GET"))
{
page = 1;
}
string table = String.IsNullOrWhiteSpace(dropDownSelection["Table"]) ? "%" : dropDownSelection["Table"];
string issue = String.IsNullOrWhiteSpace(dropDownSelection["IssueType"]) ? "%" : dropDownSelection["IssueType"];
string status = String.IsNullOrWhiteSpace(dropDownSelection["Status"]) ? "%" : dropDownSelection["Status"];
var followUpItem = from follow in db.FollowUpItems
where (follow.TableName.Equals(table) || table.Equals("%")) &&
(follow.IssueType.Equals(issue) || issue.Equals("%")) &&
(follow.Status.Equals(status) || status.Equals("%"))
orderby follow.Id
select follow;
int pageNumber = (page ?? 1);
int pageSize = 10;
return View(followUpItem.ToPagedList(pageNumber, pageSize));
}
这是视图中的下拉列表:
<select name="Table" title="Table" style="font-size:8pt;">
<option value="%">--Table Name--</option>
<option value="AgentContEd">CE</option>
<option value="AgentProductTraining">PT</option>
</select>
<select name="IssueType" style="font-size:8pt;">
<option value="%">--Issue Type--</option>
<option value="W">Warning</option>
<option value="E">Error</option>
</select>
<select name="Status" style="font-size:8pt;">
<option value="%">--Status Type--</option>
<option value="O">Open</option>
<option value="U">Under Review</option>
</select>
并且(以防万一)下面是<div>
包含视图中的 PagedList 导航按钮的:
<div>
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
of @Model.PageCount
@if (Model.HasPreviousPage)
{
@Html.ActionLink("<<", "Index", new { page = 1, sortOrder = ViewBag.CurrentSort, searchString = ViewBag.CurrentFilter, currentFilter = ViewBag.CurrentFilter })
@Html.Raw(" ");
@Html.ActionLink("< Prev", "Index", new { page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort, searchString = ViewBag.CurrentFilter, currentFilter = ViewBag.CurrentFilter })
}
else
{
@:<<
@Html.Raw(" ");
@:< Prev
}
@if (Model.HasNextPage)
{
@Html.ActionLink("Next >", "Index", new { page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort, searchString = ViewBag.CurrentFilter, currentFilter = ViewBag.CurrentFilter })
@Html.Raw(" ");
@Html.ActionLink(">>", "Index", new { page = Model.PageCount, sortOrder = ViewBag.CurrentSort, searchString = ViewBag.CurrentFilter, currentFilter = ViewBag.CurrentFilter })
}
else
{
@:Next >
@Html.Raw(" ")
@:>>
}
</div>