使用ASP对网格进行分页和排序。Net MVC

时间:2022-11-30 16:46:42

I'm new to MVC, and am not following how you'd do paging and sorting on a grid. I'm used to using the asp.Net GridView control with an ObjectDataSource pointed at objects in our business layer - and in that case the ODS handles all of the paging & sorting using the methods that our ORM generates on the objects.

我是MVC新手,不了解如何在网格上进行分页和排序。我习惯使用asp。使用指向业务层中的对象的ObjectDataSource的网格视图控件——在这种情况下,ODS使用ORM在对象上生成的方法处理所有的分页和排序。

I've looked at using the same ORM with MVC - and things work out fine there - i just loop thru the collections to build the table on the page - but without the ODS to handle the paging & sorting, i'm confused as to how I'd handle that. Would I have a separate controller for the paging and sorting?

我已经考虑过使用与MVC相同的ORM—在这里一切都很好—我只是通过循环遍历集合来在页面上构建表—但是没有ODS来处理分页和排序,我不知道如何处理它。我是否需要一个单独的控制器来进行分页和排序?

I understand that I need to roll my own - but where do I start? I've created a CustomerController, and a view that displays a table of customers that looks like below - and I want to sort on FirstName or LastName columns. My Model has a Sort() method on it that'll take a string sort expression in the format that would be used by a GridView/ODS pair. Would I create a new Action on my CustomerController called Sort, and put an ActionLink in my header?

我知道我需要自己动手——但我该从哪里开始呢?我创建了一个CustomerController和一个视图,该视图显示如下所示的客户表——我想对FirstName或LastName列进行排序。我的模型有一个Sort()方法,它将采用GridView/ODS对使用的格式的字符串排序表达式。我是否会在我的CustomerController中创建一个名为Sort的新操作,并在我的头中添加一个ActionLink ?

    <table>
    <tr>
        <th>
            First Name
        </th>
        <th>
            Last Name
        </th>
    </tr>
    <% foreach (var item in Model)
       { %>
    <tr>
        <td>
            <%= Html.Encode(item.FirstName) %>
        </td>
        <td>
            <%= Html.Encode(item.LastName) %>
        </td>
    </tr>
    <% } %>
</table>

5 个解决方案

#1


29  

Your can use the same controller, just add an additional parameter and name it sort. Then check in the controller what value sort has, and sort your data based on that parameter.

您可以使用相同的控制器,只需添加一个附加参数并命名它为sort。然后在控制器中检查值排序,并根据该参数对数据进行排序。

Or if you want to do things on the client side, you can use something like tablesorter, a plugin for jquery.

或者,如果你想在客户端做一些事情,你可以使用大型列表,jquery插件。

#2


38  

nowadays MVC 3 now has a webGrid out of the box. I know this question is from a while ago, but I ended up on it when looking for something about webGrid. So I thought it should have an answer mentioning the new webGrid.

现在MVC 3有了一个现成的webGrid。我知道这个问题是前段时间提出的,但是我在寻找webGrid时发现了这个问题。所以我认为它应该有一个关于新的webGrid的答案。

Here are a few good posts on how to use it:

以下是一些如何使用的好帖子:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=615

http://www.dotnetcurry.com/ShowArticle.aspx?ID=615

http://cnug.co.in/blogs/shijuv/archive/2010/10/08/using-the-webgrid-helper-in-asp-net-mvc-3-beta.aspx

http://cnug.co.in/blogs/shijuv/archive/2010/10/08/using-the-webgrid-helper-in-asp-net-mvc-3-beta.aspx

http://www.nickharris.net/tag/webgrid/

http://www.nickharris.net/tag/webgrid/

It supports sorting, paging and also some Ajax stuff. It can do lot for you already, but you can also specify every separate column separately.

它支持排序、分页和一些Ajax内容。它可以为您做很多事情,但是您也可以分别指定每个单独的列。

Update:
There are also many JavaScript libraries available that can do the table for you. I personally like to use DataTables. You can feed it an existing html table generated on the server or give it an endpoint where it can retrieve the data (all or just one page).

更新:还有许多可用的JavaScript库可以为您做这个表。我个人喜欢使用DataTables。您可以向它提供一个在服务器上生成的现有html表,或者给它一个端点,让它可以检索数据(全部或仅一个页面)。

There are plenty more, just Google around.

还有很多,只是谷歌。

#3


3  

With MVC you sort of have to roll your own sorting, paging, etc. I would suggest YUI DataTable or some of the other JavaScript Grids out there.

有了MVC,你就必须自己进行排序、分页等等。我建议YUI DataTable或者其他一些JavaScript网格。

Also if you find your self doing heavy Data Grid work you may want to take a look at ASP.NET Dynamic Data, it is specifically designed for these types of interactions against ORM's.

另外,如果您发现自己正在做大量的数据网格工作,您可能想看看ASP。NET动态数据,它是专门为针对ORM的这些类型的交互设计的。

#4


2  

First use jQuery. jQuery is your friend. Then use this awesome and probably the best Grid control for jQuery jqGrid.

第一次使用jQuery。jQuery是你的朋友。然后使用这个很棒的,可能是jQuery jqGrid最好的网格控件。

In your CustomerController create an action called CustomerData. All interaction with the Grid should point to this action.

在CustomerController中创建一个名为CustomerData的操作。与网格的所有交互都应该指向这个操作。

Go here for tons of examples on how to use jqGrid.

这里有大量关于如何使用jqGrid的示例。

#5


0  

We have been using client side control JqxGrid from JqWidgets and much satisfied with its performance related to huge number of records along with paging, filtering, sorting inbuilt within. Here is an example of binding it within ASP.Net MVC

我们一直在使用JqWidgets的客户端控件JqxGrid,并对其与大量记录相关的性能非常满意,以及内部的分页、过滤和排序。这里有一个在ASP中绑定它的例子。Net MVC

#1


29  

Your can use the same controller, just add an additional parameter and name it sort. Then check in the controller what value sort has, and sort your data based on that parameter.

您可以使用相同的控制器,只需添加一个附加参数并命名它为sort。然后在控制器中检查值排序,并根据该参数对数据进行排序。

Or if you want to do things on the client side, you can use something like tablesorter, a plugin for jquery.

或者,如果你想在客户端做一些事情,你可以使用大型列表,jquery插件。

#2


38  

nowadays MVC 3 now has a webGrid out of the box. I know this question is from a while ago, but I ended up on it when looking for something about webGrid. So I thought it should have an answer mentioning the new webGrid.

现在MVC 3有了一个现成的webGrid。我知道这个问题是前段时间提出的,但是我在寻找webGrid时发现了这个问题。所以我认为它应该有一个关于新的webGrid的答案。

Here are a few good posts on how to use it:

以下是一些如何使用的好帖子:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=615

http://www.dotnetcurry.com/ShowArticle.aspx?ID=615

http://cnug.co.in/blogs/shijuv/archive/2010/10/08/using-the-webgrid-helper-in-asp-net-mvc-3-beta.aspx

http://cnug.co.in/blogs/shijuv/archive/2010/10/08/using-the-webgrid-helper-in-asp-net-mvc-3-beta.aspx

http://www.nickharris.net/tag/webgrid/

http://www.nickharris.net/tag/webgrid/

It supports sorting, paging and also some Ajax stuff. It can do lot for you already, but you can also specify every separate column separately.

它支持排序、分页和一些Ajax内容。它可以为您做很多事情,但是您也可以分别指定每个单独的列。

Update:
There are also many JavaScript libraries available that can do the table for you. I personally like to use DataTables. You can feed it an existing html table generated on the server or give it an endpoint where it can retrieve the data (all or just one page).

更新:还有许多可用的JavaScript库可以为您做这个表。我个人喜欢使用DataTables。您可以向它提供一个在服务器上生成的现有html表,或者给它一个端点,让它可以检索数据(全部或仅一个页面)。

There are plenty more, just Google around.

还有很多,只是谷歌。

#3


3  

With MVC you sort of have to roll your own sorting, paging, etc. I would suggest YUI DataTable or some of the other JavaScript Grids out there.

有了MVC,你就必须自己进行排序、分页等等。我建议YUI DataTable或者其他一些JavaScript网格。

Also if you find your self doing heavy Data Grid work you may want to take a look at ASP.NET Dynamic Data, it is specifically designed for these types of interactions against ORM's.

另外,如果您发现自己正在做大量的数据网格工作,您可能想看看ASP。NET动态数据,它是专门为针对ORM的这些类型的交互设计的。

#4


2  

First use jQuery. jQuery is your friend. Then use this awesome and probably the best Grid control for jQuery jqGrid.

第一次使用jQuery。jQuery是你的朋友。然后使用这个很棒的,可能是jQuery jqGrid最好的网格控件。

In your CustomerController create an action called CustomerData. All interaction with the Grid should point to this action.

在CustomerController中创建一个名为CustomerData的操作。与网格的所有交互都应该指向这个操作。

Go here for tons of examples on how to use jqGrid.

这里有大量关于如何使用jqGrid的示例。

#5


0  

We have been using client side control JqxGrid from JqWidgets and much satisfied with its performance related to huge number of records along with paging, filtering, sorting inbuilt within. Here is an example of binding it within ASP.Net MVC

我们一直在使用JqWidgets的客户端控件JqxGrid,并对其与大量记录相关的性能非常满意,以及内部的分页、过滤和排序。这里有一个在ASP中绑定它的例子。Net MVC