在.NET 4.0中,WebGrid + Paging + Sorting + Filtering中的过滤器丢失了

010*_*101 14 c# asp.net-mvc webgrid asp.net-mvc-3

我已经实现了一个WebGrid.排序,分页和过滤不能一起使用.它们在您单独使用时起作用.当您将三者结合使用时,过滤不起作用.

症状:
过滤结果集,然后排序.

要么

过滤结果集,然后转到下一页.

在这两种情况下,过滤器都会丢失.但它确实是页面和排序.

在后面的代码中:当通过排序或分页调用action方法时,为每个过滤器参数显示null.

通过过滤器调用操作方法时,过滤器参数会通过.

这告诉我,当你开始排序或分页时,它没有提交表单.

public ActionResult MyPage(int? page, int? rowsPerPage, 
              string sort, string sortdir, 
              string orderNumber, string person, string product)
Run Code Online (Sandbox Code Playgroud)

我在SO和其他地方环顾四周.有很多例子和人们会问如何做一个或另一个或全部三个.但我只看到一个与我的问题,所以我在这里发布.(他也没有解决)

我的页面实现如下:

@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>

}
Run Code Online (Sandbox Code Playgroud)

网格实现为这样的局部视图:

<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 }))
            )
        );
}
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 24

在构建分页和排序链接时,WebGrid帮助程序会考虑当前URL中存在的所有查询字符串参数.它忽略了POSTed和路由值.由于您的搜索表单POST,用户在此表单中输入的值不存在于查询字符串中,因此它们不是分页和排序链接的一部分,当您单击其中一个链接时,值为丢失.这是设计的.

所以解决这个问题的一种方法是替换你的AjaxForm:

@using (Ajax.BeginForm("MyPage", null, new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "myGrid" }, new { id = "filter" }))
Run Code Online (Sandbox Code Playgroud)

使用GET动词的标准HTML表单:

@using (Html.BeginForm("MyPage", null, FormMethod.Get))
Run Code Online (Sandbox Code Playgroud)

或使用GET动词的AJAX表单:

@using (Ajax.BeginForm("MyPage", null, new AjaxOptions { HttpMethod = "Get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "myGrid" }, new { id = "filter" }))
Run Code Online (Sandbox Code Playgroud)

现在,当用户想要过滤某些内容并点击"搜索"提交按钮时,他在搜索表单中输入的值将最终出现在查询字符串中,并且在呈现WebGrid帮助程序时将使用它们来生成其"排序"和"页面"链接,当然单击这些链接,值将被发送到服务器.

如果您想要对此进行更多控制,可以考虑使用更高级的网格控件,例如MvcContrib.GridTelerik Grid for ASP.NET MVC.