我有一个使用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));
}
以下是View中的下拉列表:
<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>
以下(以防万一)下面是包含View中的PagedList导航按钮的<div>
:
<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>
答案 0 :(得分:0)
如果您首先通过ViewModel传递followUpItem然后实际传递下拉列表中的选定值,该怎么办?也许接近这个。
public ActionResult Index(FormCollection dropDownSelection, string currentFilter, int? page)
{
//Stuff abbreviated
return View(new StuffViewModel(followUpItem.ToPagedList(pageNumber, pageSize)), "SelectedDropDownItem");
}
然后你可以在ViewModel中处理它,例如在内存中创建你自己的DropDownlist。这样的事情。
public class StuffViewModel
{
public List<FollowUpItem> FollowUpItems {get; private set;}
public List<SelectListItem> DropDownList {get; private set}
public StuffViewModel(List<FollowUpItem> followUpItems, string selectedDropDownValue)
{
FollowUpItems = followUpItems;
List<SelectListItem> selectList = new List<SelectListItem>();
//TODO: here you would decide which is selected from the selectedDropDownValue parameter
selectList.Add(new SelectListItem{ Selected = true, Text = "Text", Value = "Value" });
selectList.Add(new SelectListItem{ Selected = false, Text = "Text2", Value = "Value2" });
//...and more values as you need them
DropDownList = selectList
}
}
通过使用ViewModel执行此操作,无论您在pagedList中的哪个位置,您都将始终从每次单击的下拉列表中保留所选值。
请记住,您的View现在将包含一个包含StuffViewModel的模型,而不是仅包含PagedList。现在,您可以在视图中调用元素,如下所示:
@model StuffViewModel
//TODO: access dropdownlist
@Html.DropDownList("name", Model.DropDownList)
//TODO: access PagedList for instenace like this:
if(Model.FollowUpItems.HasPreviousPage)
{
//do stuff
}
我希望我有道理:)
答案 1 :(得分:0)
这很简单。您没有坚持下拉选项。分页链接需要包含当前的下拉选项,这样当您向下一页提交GET请求时,数据将在查询字符串中传递,以用于呈现视图。