带有过滤结果的网络网格
我喜欢 Razor-ViewEngine 中的 Webgrid HTML-Helper。
不幸的是我遇到了一个问题。使用内置的排序和分页功能,我失去了过滤器设置。
在网格上方,我有一个过滤器控件(表单),通过填充过滤器类并在提交时调用控制器上的操作方法来过滤网格中显示的行。
控制器中的代码:
public ActionResult DeliveryContractList(SampleClassFilter filter){
// Populate a filtered List of Items to Show
IList<SampleClass> model = FillList(filter);
// Return a View with the List.
return View("SampleClassList", model);
}
视图:
@model IEnumerable<SampleClass>
@{
ViewBag.Title = "SampleClassList";
}
@using(Html.BeginForm()){
// Form to Set the Filter
...
}
<div id = "SampleClassList">
var grid = new WebGrid(Model, canPage: true, canSort: true, ajaxUpdateContainerId: "SampleClassList");
@grid.GetHtml(htmlAttributes: new {id = "gridSampleClass"}, columns:
grid.Columns(
grid.Column("Foo", "For", canSort: true),
grid.Column("Bar", "Bar", canSort: true),
)
);
</div>
我认识到,当单击网格的列标题对控制器中的操作方法进行排序时,也会被调用。当然,过滤器类为 null 会导致未过滤的结果集。
有没有办法在对网络网格进行排序或分页时保持对结果的过滤? 我不想使用jquery!
提前致谢 Tobi
是否可以定义添加到单击排序或页面链接时调用的链接的参数?
I like the Webgrid HTML-Helper in Razor-ViewEngine.
Unfortunatly I am facing a problem. Using the built-in sorting and paging functions I loose my filter settings.
Above the grid I have a filtercontrol (form), filtering the rows shown in the grid by filling a filter-class and calling an actionmethode on the controller when submitting.
Code in Controller:
public ActionResult DeliveryContractList(SampleClassFilter filter){
// Populate a filtered List of Items to Show
IList<SampleClass> model = FillList(filter);
// Return a View with the List.
return View("SampleClassList", model);
}
The View:
@model IEnumerable<SampleClass>
@{
ViewBag.Title = "SampleClassList";
}
@using(Html.BeginForm()){
// Form to Set the Filter
...
}
<div id = "SampleClassList">
var grid = new WebGrid(Model, canPage: true, canSort: true, ajaxUpdateContainerId: "SampleClassList");
@grid.GetHtml(htmlAttributes: new {id = "gridSampleClass"}, columns:
grid.Columns(
grid.Column("Foo", "For", canSort: true),
grid.Column("Bar", "Bar", canSort: true),
)
);
</div>
I recognized that when clicking e.g. the column header of the grid to sort the action methode in the controller is called too. Of course the filter class is null leading to an unfiltered resultset.
Is there a way to keep my results filtered when sorting or paging the webgrid?
I don't want to use jquery!!!
Thanks in advance
Tobi
Is it possible to define parameters added to the link called when clicking on a sort or page link?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
啊哈。问题是您需要在过滤器字段和 Web 网格周围添加 GET 表单。然后,您需要对网络网格数据进行一些操作,以便它以它可以识别的方式将其排序信息发回。我丢失了我读到的有关此问题的文章的链接,但我这里有一个示例: ASP.NET MVC3 WebGrid - 自定义服务器端排序
您最终会得到类似 localhost/example/admin/thing?thingName=Hyatt&City=&State=TX&Country 的 URL =&sort=城市&sortdir=ASC
Ah ha. The problem is that you need to add a GET form around both your filter fields and the web grid. And then you need to do a tiny bit of massaging the webgrid data so that it posts its sorting info back in a way that it recognizes. I lost the link to the article I read about this, but I have an example here: ASP.NET MVC3 WebGrid - custom, server-side sorting
You end up with urls like localhost/example/admin/thing?thingName=Hyatt&City=&State=TX&Country=&sort=city&sortdir=ASC