寻呼使用 ASP.Net MVC 对网格进行排序 [英] Paging & Sorting grids with ASP.Net MVC

查看:16
本文介绍了寻呼使用 ASP.Net MVC 对网格进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 MVC 的新手,并没有关注您如何在网格上进行分页和排序.我习惯于使用 asp.Net GridView 控件和一个 ObjectDataSource 指向我们业务层中的对象 - 在这种情况下,ODS 处理所有分页 &使用我们的 ORM 在对象上生成的方法进行排序.

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 中使用相同的 ORM - 那里的情况很好 - 我只是循环遍历集合以在页面上构建表 - 但没有 ODS 来处理分页 &排序,我很困惑我将如何处理.我会有一个单独的控制器用于分页和排序吗?

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?

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

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?

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

推荐答案

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

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.

或者如果你想在客户端做一些事情,你可以使用像tablesorter这样的插件,一个插件用于 jquery.

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

这篇关于寻呼使用 ASP.Net MVC 对网格进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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