C# 过滤器在 .NET 4.0 中的 WebGrid + Paging + Sorting + Filtering 中迷失了方向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10051794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filter is getting lost in WebGrid + Paging + Sorting + Filtering in .NET 4.0
提问by 010110110101
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.
我已经实现了一个 WebGrid。排序、分页和过滤不能一起工作。当您单独使用它们时,它们会起作用。当您将三者同时组合时,过滤不起作用。
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.
在后面的代码中:当通过排序或分页调用 action 方法时,每个过滤器参数都会显示空值。
When the action method is called via the filter, the filter parameters come through.
当通过过滤器调用 action 方法时,过滤器参数会通过。
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 }))
)
);
}
采纳答案by Darin Dimitrov
When building the pagination and sort links, the WebGrid helper takes into account all query string parameterspresent in the current url. It ignores POSTed and route values. And since your Search form POSTs, the values that have been entered by the user in this form are not present in the query string, so they are not part of the pagination and sort links and when you click on one of those links the values are lost. This is by design.
在构建分页和排序链接时,WebGrid 助手会考虑当前 url 中存在的所有查询字符串参数。它忽略 POSTed 和路由值。并且由于您的搜索表单 POST,用户在此表单中输入的值不存在于查询字符串中,因此它们不是分页和排序链接的一部分,当您单击这些链接之一时,这些值是丢失。这是设计使然。
So one way to fix that is to replace your AjaxForm:
所以解决这个问题的一种方法是替换你的 AjaxForm:
@using (Ajax.BeginForm("MyPage", null, new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "myGrid" }, new { id = "filter" }))
with a standard HTML form using the GET verb:
使用 GET 动词的标准 HTML 表单:
@using (Html.BeginForm("MyPage", null, FormMethod.Get))
or an AJAX form using the GET verb:
或使用 GET 动词的 AJAX 表单:
@using (Ajax.BeginForm("MyPage", null, new AjaxOptions { HttpMethod = "Get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "myGrid" }, new { id = "filter" }))
Now, when the user wants to filter something and hits the Search submit button the values he entered in the search form will end up in the query string and when rendering the WebGrid helper will use them to generate its Sort and Page links and of course when you click on those links the values will be sent to the server.
现在,当用户想要过滤某些内容并点击搜索提交按钮时,他在搜索表单中输入的值将最终出现在查询字符串中,并且在呈现时 WebGrid 助手将使用它们来生成其排序和页面链接,当然当您单击这些链接,这些值将被发送到服务器。
If you want more control over this you might consider more advanced grid controls such as MvcContrib.Gridor the Telerik Grid for ASP.NET MVC.
如果您想对此进行更多控制,您可以考虑使用更高级的网格控件,例如MvcContrib.Grid或用于 ASP.NET MVC的Telerik Grid。
回答by Karthik Chintala
Submit the form to the URL that the paging link points to:
将表单提交到分页链接指向的 URL:
<script type="text/javascript">
$(function () {
$('th a, tfoot a').click(function () {
$('form').attr('action', $(this).attr('href')).submit();
return false;
});
});
</script>
This doesnot help me but might help you
这对我没有帮助,但可能对你有帮助
回答by Utkarsh Bais
Just create a GET for your action method, whenever a sort or paging fires from the grid it hits the GET method along with a lot of parameters (You can see by pointing at the paging number or sort header of the grid using web developer tools of your browser), there you can filter your dataset and then pass the model to the view :
只需为您的操作方法创建一个 GET,无论何时从网格触发排序或分页,它都会命中 GET 方法以及许多参数(您可以使用 Web 开发人员工具指向网格的分页编号或排序标题来查看您的浏览器),您可以在那里过滤数据集,然后将模型传递给视图:
[HttpGet]
public ActionResult MyPage()
Everytime you do Sort or Paging this method would be hit, then you can do your filtering, you could add some static flags which can be assigned depending on what you want to filter.
每次您执行排序或分页时,都会命中此方法,然后您可以进行过滤,您可以添加一些静态标志,这些标志可以根据您要过滤的内容进行分配。

