我已经建立了一个 webgrid,它似乎工作正常,允许我进行排序和分页。我添加了一个过滤器选项,它也很有效,但是如果我过滤然后对结果进行排序,过滤器就会丢失并且所有记录都会显示出来。
这是我的 Razor 视图代码:
@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "Get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "myGrid" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Document Search</legend>
<div class="editor-label">
@Html.Label("Enter a Document code:")
</div>
<div class="editor-field">
@Html.Editor("search")
</div>
<p>
<input type="submit" value="Search" />
</p>
</fieldset>
}
@{
WebGrid grid = new WebGrid(null, rowsPerPage: 10, canPage: true, canSort: true, ajaxUpdateContainerId: "myGrid");
grid.Bind(Model, autoSortAndPage: true);
}
<div id="myGrid">
@grid.GetHtml(mode: WebGridPagerModes.All, firstText: "First Page", nextText: "Next", previousText: "Previous", lastText: "Last Page", numericLinksCount: 10,
columns: grid.Columns(
grid.Column("DocumentID", "Document Code", canSort: true),
grid.Column("Title", "Document Title", canSort: true)
)
)
</div>
这是我的行动:
public ActionResult Index(string search)
{
List<DocumentIndexViewModel> viewModel = Mapper.Map<List<DocumentIndexViewModel>>(DocumentService.GetDocumentsBySearch(search));
if (Request.IsAjaxRequest())
return PartialView("_IndexGrid", viewModel);
else
return View(viewModel);
}
对显示的记录进行排序时如何维护过滤器?似乎我需要以某种方式将搜索字符串附加到排序链接上,但不确定如何继续。