如何做到在MVC应用程序分页和过滤 [英] How to do pagination and filtering in MVC applications

查看:134
本文介绍了如何做到在MVC应用程序分页和过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的例子的Martijn博兰的

我的应用程序的URL改变了,当我点击页码:

 的http://本地主机:1202 /第页= 6

到目前为止好。

问题是,当我再补充一点变化上提交一个DropDownList,我不能页面以及过滤器。

如果我改变我的下拉列表中,我应该通过专案编号在querysting,我partail视图将刷新并显示我的过滤recoreds。

现在,当我第滤波器参数不会在查询字符串坚持,以及在我的下拉不沾的选择。

如何页面以及过滤器?

的Index.aspx

 <表ID =表单提交项目选择级=后评价的方法=获取>
 <标签=国家>项目:其中; /标签>
 &所述;%= Html.DropDownList(ProjectList,--All--)%>
< /表及GT;<%Html.RenderPartial(MyIssues模型); %GT;<脚本类型=文/ JavaScript的>
         $(#表单后项目的选择)。递交(函数(EVT){
         VAR FRM = $(#表单提交项目选择);
         VAR行动= frm.attr(行动);
         变种serializedForm = frm.serialize();
         VAR专案编号= jQuery.trim($(ProjectList)VAL());
         如果(projectId.length&。1 ||专案编号==-1)
             返回;
     });
     $(#ProjectList)。改变(函数(){
         $(#表单项目后选择)提交();
     });
< / SCRIPT>


我觉得你有几个选择这里。

第一个将要使用Ajax回打电话到控制器获得了分页对象集合回来,每次您筛选回传过滤器ID来获得您所需要给你的页面的结果集的输出想。你可以做与阿贾克斯以及回传过滤器ID与Ajax调用所以你会得到过滤和分页对象返回或者你可以重定向到一个不同的URL下面分页介绍分页。

如果您使用整页回发到服务器上的下拉菜单更改值,那么你可以将用户重定向到与它如过滤器ID的另一个网址。发行/过滤/ 1

每个分页链接使用,你最终会像发行/过滤/ 1?页= 6一个URL,你不会失去你的过滤时间这种方式。

请注意页面视图不需要变化

所有虽然函数结束了相同的答案,这个问题是在下面的意见,并张贴在这里为方便。

 <%= Html.Pager(ViewData.Model.PageSize,ViewData.Model.PageNumber,ViewData.Model.TotalItemCount,新{类别名称=计算机[CategoryDisplayName]}) %GT;

I am using the example martijn Boland's to page.

The url of my app changes when I click on the page numbers:

http://localhost:1202/Issue?page=6

So far so good.

The problem is when I add a dropdownlist that submits on change, I can not page as well as filter.

If I change my dropdown, I should pass the projectId in the querysting, my partail view will refresh and display my filtered recoreds.
Now when I page the filter parameter does not stick in the querystring as well as the selection in my dropdown does not stick.

How can I page as well as filter?

Index.aspx

<form id="form-post-project-selection" class="post-comments" method="get">
 <label for="Country">Project:</label>
 <%= Html.DropDownList("ProjectList", "--All--") %>
</form>

<% Html.RenderPartial("MyIssues", Model); %>

<script type="text/javascript">
         $("#form-post-project-selection").submit(function(evt) {
         var frm = $("#form-post-project-selection");
         var action = frm.attr("action");
         var serializedForm = frm.serialize();
         var projectId = jQuery.trim($("ProjectList").val());
         if (projectId.length < 1 || projectId == "-1")
             return;
     });
     $("#ProjectList").change(function() {
         $("#form-post-project-selection").submit();
     });
</script>

解决方案

I think you have a couple of options here.

The first one would to to use Ajax to call back to a controller to get a "Paged" object collection back so every time you filter the output you postback the filter ID to get only the results collections you need for the page you want. You could do the paging with Ajax as well passing back the Filter ID with the Ajax call so you will get the filtered and paged object back or you could redirect to a different url on paging explained below.

If you use full page postback to the server on changing the value in the dropdown then you could redirect the user to another url with the filter id in it eg. Issue/Filter/1

This way every time the paging links are used you will end up with a url like Issue/Filter/1?page=6 and you would not loose your filter.

Please note the view of the page does not need to vary.

All though the function ends up the same the answer to this question is in the comments below and posted here for ease.

<%= Html.Pager(ViewData.Model.PageSize, ViewData.Model.PageNumber, ViewData.Model.TotalItemCount, new { categoryname = ViewData["CategoryDisplayName"] } )%>

这篇关于如何做到在MVC应用程序分页和过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆