I've implemented a WebGrid. Sorting, paging and filtering do not work together. They work when you use them alone. When you combine the three, at the same time, filtering doesn't work.
The symptom:
Filter the resultset, then sort.
or
Filter the resultset, then go to next page.
In both cases, the filter is lost. But it does page and sort.
In the code behind:
When the action method is called via a sort or pagination, nulls show for each of the filter parameters.
When the action method is called via the filter, the filter parameters come through.
This tells me that when you initiate a sort or a pagination that it's not submitting the form.
public ActionResult MyPage(int? page, int? rowsPerPage,
string sort, string sortdir,
string orderNumber, string person, string product)
I've looked around on SO and elsewhere. There are plenty of examples and people asking how to do one or another or all three. But I have only seen one with my issue, so I'm posting it here. (his was unsolved as well)
I have my page implemented as follows:
@using (Ajax.BeginForm("MyPage", null, new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "myGrid" }, new { id = "filter" }))
{
<div class="right">
<select id="rowsPerPage" name="rowsPerPage">
<option>15</option>
<option>25</option>
<option>50</option>
<option>75</option>
<option>100</option>
</select>
</div>
<div class="table">
<div class="tableRow">
<div class="tableCell">
Order Number
</div>
<div class="tableCell">
Person
</div>
<div class="tableCell">
Product
</div>
</div>
<div class="tableRow">
<div class="tableCell">
<input type="text" id="orderNumber" name="orderNumber" />
</div>
<div class="tableCell">
<input type="text" id="person" name="person" />
</div>
<div class="tableCell">
<input type="text" id="product" name="product" />
</div>
<div class="tableCell">
<input type="submit" class="button" value="Search" />
</div>
</div>
</div>
<br/>
<div id="myGrid">
@Html.Partial("_MyPage", Model)
</div>
}
The grid is implemented as a partial view like this:
<script type="text/javascript">
$(document).ready(function () {
resetUI();
});
</script>
@{
var grid = new WebGrid(canPage: true, rowsPerPage: Model.rowsPerPage, canSort: true, ajaxUpdateContainerId: "grid", ajaxUpdateCallback: "resetUI");
grid.Bind(Model.rows, rowCount: Model.TotalRecords, autoSortAndPage: false);
@grid.GetHtml(
tableStyle: "fancyTable",
headerStyle: "header",
footerStyle: "footer",
rowStyle: "row",
alternatingRowStyle: "alt",
mode: WebGridPagerModes.Numeric | WebGridPagerModes.NextPrevious,
nextText: "Next",
previousText: "Previous",
htmlAttributes: new { id = "grid" },
columns: grid.Columns(
grid.Column("OrderDate", "Order Date", format: @<text>@((item.OrderDate != null) && (item.OrderDate.ToString("MM/dd/yyyy") != "01/01/0001") ? item.OrderDate.ToString("MM/dd/yyyy") : "")</text>),
grid.Column("OrderNumber", "Order Number"),
grid.Column("Field1, "Field 1"),
grid.Column("Field2", "Field 2"),
grid.Column("Person", "Person"),
grid.Column("Product", "Product"),
grid.Column(format: (item) => Html.ActionLink("View", "Details", new { id = item.orderNumber }))
)
);
}
See Question&Answers more detail:
os