带有复选框过滤器的 MVC 3 WebGrid
我正在使用 MVC 3 RTM 开发一个 Web 应用程序。在视图中,我有一个 WebGrid,它可以很好地进行排序和分页。但是,我还需要页面上的一些过滤支持,因此我添加了一个文本框和一个复选框。视图代码如下所示:
@using (Html.BeginForm("Index", "Customer", FormMethod.Get))
{
<fieldset>
<legend>Filter</legend>
<div class="display-label">
Search for</div>
<div class="display-field">@Html.TextBox("filter", null, new { onchange = "$('form').submit()" })</div>
<div class="display-label">
Show inactive customers?
</div>
<div class="display-field">
@Html.CheckBox("showInactive", false, new { onchange = "$('form').submit()" })
</div>
</fieldset>
var grid = new WebGrid(canPage: true, canSort: true, ajaxUpdateContainerId: "grid", rowsPerPage: 10, defaultSort: "Name");
grid.Bind(Model, rowCount: Model.Count, autoSortAndPage: false);
grid.Pager(WebGridPagerModes.All);
@grid.GetHtml(htmlAttributes: new { id = "grid" },
columns: grid.Columns(
grid.Column("Name", "Name"),
grid.Column("Address", "Address"),
grid.Column("IsActive", "Active", (item) => item.IsActive ? "Yes" : "No")));
}
除非我选中复选框,否则效果很好。当我第一次加载页面时,该复选框未被选中。排序和分页有效,我可以输入一些文本作为过滤器,它会过滤我的结果,之后排序和分页仍然有效。但是,如果我选中该复选框,它会更新结果,但排序不再有效。不过,过滤器仍然有效,因此如果我输入一些文本,它会正确过滤结果并且仍然遵循复选框。
我尝试在控制器中设置断点,没有问题。当我尝试排序时发送请求,控制器正确返回视图,并将结果作为模型。但 WebGrid 似乎并没有自我更新。
有没有其他人经历过这种情况,或者有人对寻找什么有一些好的建议?
谢谢!
I'm developing a web application using MVC 3 RTM. In a view I have a WebGrid which works fine with sorting and paging. However, I also needed some filtering support on the page, so I added a textbox and a checkbox. The view code looks like this:
@using (Html.BeginForm("Index", "Customer", FormMethod.Get))
{
<fieldset>
<legend>Filter</legend>
<div class="display-label">
Search for</div>
<div class="display-field">@Html.TextBox("filter", null, new { onchange = "$('form').submit()" })</div>
<div class="display-label">
Show inactive customers?
</div>
<div class="display-field">
@Html.CheckBox("showInactive", false, new { onchange = "$('form').submit()" })
</div>
</fieldset>
var grid = new WebGrid(canPage: true, canSort: true, ajaxUpdateContainerId: "grid", rowsPerPage: 10, defaultSort: "Name");
grid.Bind(Model, rowCount: Model.Count, autoSortAndPage: false);
grid.Pager(WebGridPagerModes.All);
@grid.GetHtml(htmlAttributes: new { id = "grid" },
columns: grid.Columns(
grid.Column("Name", "Name"),
grid.Column("Address", "Address"),
grid.Column("IsActive", "Active", (item) => item.IsActive ? "Yes" : "No")));
}
This works fine except when I check the checkbox. When I load the page for the first time, the checkbox is not checked. Sorting and paging works, and I can enter some text as a filter and it filters my result, and sorting and paging still works after that. However, if I check the checkbox, it updates the result, but sorting no longer works. The filter still works though, so if I enter some text, it correctly filters the result and still respects the checkbox.
I've tried setting a breakpoint in my controller, and there's no problem there. The request is sent when I try to sort, and the controller correctly returns the view with the result as the model. But it doesn't seem like the WebGrid is updating itself.
Has anyone else experienced this, or has anyone some good advice on what to look for?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通常添加一个表单(在我的 WebGrid 上方),其中包含一个名为“Keywords”的文本框和一个名为“IsActive”的复选框,当单击“Go”按钮时,它会重新加载 WebGrid,将“Keywords”和“IsActive”选项添加到查询字符串。
您可以向表单添加更多元素,它们的值将被发送。
使用以下帮助程序脚本 - webgrid.helper.js:
然后,在我的 webgrid 上方,我有以下部分文件 - *_ListFilter。 html*
I normally add a form (above my WebGrid) which contains a textbox called "Keywords" and a checkbox called "IsActive" and when the "Go" button is clicked it reloads the WebGrid adding the "Keywords" and "IsActive" options to the query string.
You can add more elements to your form and their values will be sent al
Use the following helper script - webgrid.helper.js:
Then just above my webgrid, I have the following partial file - *_ListFilter.cshtml*