I have a Search.cshtml View, that looks like that:
@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 }))
)
)
}
My Controller has the following methods:
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);
}
Everything works almost fine. But the sorting of the Columns and the Paging-Links dont work. If you click on one of them, the View reloads with no data in the Grid.
Is there any way to get the WebGrid work in this Search-View?