我有一个 Search.cshtml 视图,看起来像这样:
@model IEnumerable<MVC.Models.Books>
@using (Html.BeginForm())
{
@Html.DropDownList("Categorie", ViewBag.Categorie as SelectList);
@Html.TextBox("Query");
<input type="submit" value="Search" />
}
@if (this.Model.Any())
{
var grid = new WebGrid(Model, rowsPerPage:20);
@grid.GetHtml(mode: WebGridPagerModes.All,
columns: grid.Columns(
grid.Column(columnName: "Number"),
grid.Column(columnName: "Title"),
grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.Nummer })),
grid.Column(format: (item) => Html.ActionLink("Details", "Details", new { id = item.Nummer })),
grid.Column(format: (item) => Html.ActionLink("Delete", "Delete", new { id = item.Nummer }))
)
)
}
我的控制器有以下方法:
public ActionResult Search()
{
ViewBag.Categorie= new SelectList(new[] { "Number", "Title"});
IEnumerable<Books> model = new List<Books>();
return View(model);
}
[HttpPost]
public ActionResult Search(string categorie, string query)
{
ViewBag.Categorie= new SelectList(new[] { "Number", "Title"});
IEnumerable<Books> model;
switch (categorie)
{
case "Number":
int qnumber = Convert.ToInt32(query);
model = _db.Books.Where(b => b.Number == qnumber)
.OrderBy(b => b.Number).ToList();
break;
default:
model = _db.Books.Where(b => b.Title.Contains(query))
.OrderBy(b => b.Title).ToList();
break;
}
return View(model);
}
一切正常。但是列和分页链接的排序不起作用。如果单击其中一个,视图将重新加载,而网格中没有数据。
有没有办法让 WebGrid 在这个搜索视图中工作?