动态模型排序

时间:2014-04-26 23:16:15

标签: c# asp.net-mvc entity-framework asp.net-mvc-4

不可否认,我是MVC的全新手,我在这方面遇到了很大的困难。

我的数据库表由IEntryRepository实例化到Entry Controller中的Entity Framework。我可以提取数据,我的分页助手似乎正在运行。我甚至可以过滤结果。但是我很难搞清楚如何对列进行排序。

来自控制器:

   public ViewResult Entries(string Specialty, string sortOrder, string CurrentSort, int page = 1)
    {
        //ViewBag.CurrentSort = sortOrder;

        //sortOrder = String.IsNullOrEmpty(sortOrder) ? "TimeStamp" : sortOrder;

        //for testing
        sortOrder = "Case";

        switch (sortOrder)
        {
            case "Case":
                sortOrder = "p.Case";
                break;

            case "Timestamp":
                sortOrder = "p.TimeStamp";
                break;

            case "Origin":
                sortOrder = "Origin";
                break;

            case "AssignedTo":
                sortOrder = "AssignedTo";
                break;

            case "AssignedBy":
                sortOrder = "AssignedBy";
                break;
        }

        EntryListViewModel model = new EntryListViewModel
        {
            Entry = repository.Entries.AsQueryable()
            .Where(p => Specialty == null || p.Sub == Specialty)
            .OrderBy(p => sortOrder)
            .Skip((page - 1) * pagesize)
            .Take(pagesize),
            PagingInfo = new PagingInfo
            {
                CurrentPage = page,
                ItemsPerPage = pagesize,
                TotalItems = Specialty == null ?
                repository.Entries.Count() :
                repository.Entries.Where(p => p.Sub == Specialty).Count()
            },

            CurrentCategory = Specialty
        };


        return View(model);
    }

从视图:

@model ProjectSquid.WebUI.Models.EntryListViewModel


<table class="entries">
    <thead>
        <tr>
            <th>@Html.ActionLink("Case Number", "Entries", "Entry", new {Specialty = Model.CurrentCategory, sortOrder = "Case", CurrentSort = ViewBag.CurrentSort}) </th>
            <th>Time Stamp </th>
            <th>Origin </th>
            <th>Assigned To </th>
            <th>Assigned By </th>
            <th>Customer Name </th>
            <th>Customer Phone </th>
            <th>Comments </th>
        </tr>
    </thead>
    <tbody class="entry">
        @foreach (var p in Model.Entry)
        {
            <tr>
                <td title="@p.Case">@p.Case</td>
                <td title="@p.TimeStamp">@p.TimeStamp</td>
                <td title="@p.Origin">@p.Origin</td>
                <td title="@p.AssignedTo">@p.AssignedTo</td>
                <td title="@p.AssignedBy">@p.AssignedBy</td>
                <td title="@p.CustomerName">@p.CustomerName</td>
                <td title="@p.CustomerPhone">@p.CustomerPhone</td>
                <td title="@p.Comments">@p.Comments</td>
                @if (Context.User.Identity.IsAuthenticated){<td>Delete</td>}
            </tr>}
    </tbody>
</table>

<div class="pager">
        @Html.PageLinks(Model.PagingInfo, x => Url.Action("Entries",
        new { page = x, category = Model.CurrentCategory }))
</div>

我知道这里有一堆错误,但我的主要问题是.OrderBy为什么不接受我传递给它的任何内容?这是使用Lambda的不正确方法吗?怎么办呢?

我发现了不少教程,但似乎都没有处理对实体框架模型的排序。

任何帮助将不胜感激,谢谢!

编辑(固定):

控制器:

    public ViewResult Entries(string Specialty, string sortOrder, int page = 1)
    {
        //ViewBag.CurrentSort = sortOrder;

        //sortOrder = String.IsNullOrEmpty(sortOrder) ? "TimeStamp" : sortOrder;

        var Query = repository.Entries
            .Where(p => Specialty == null || p.Sub == Specialty);

        switch (sortOrder)
        {
            default:
                Query = Query.OrderByDescending(p => p.TimeStamp);
                break;

            case "Case":
                Query = Query.OrderBy(p => p.Case);
                break;

            case "Timestamp":
                Query = Query.OrderBy(p => p.TimeStamp);
                break;

            case "Origin":
                Query = Query.OrderBy(p => p.Origin);
                break;

            case "AssignedTo":
                Query = Query.OrderBy(p => p.AssignedTo);
                break;

            case "AssignedBy":
                Query = Query.OrderBy(p => p.AssignedBy);
                break;
        }

        EntryListViewModel model = new EntryListViewModel
        {
            Entry = Query
            //.Where(p => Specialty == null || p.Sub == Specialty)
            //.OrderBy(p => sortOrder)
            .Skip((page - 1) * pagesize)
            .Take(pagesize),
            PagingInfo = new PagingInfo
            {
                CurrentPage = page,
                ItemsPerPage = pagesize,
                TotalItems = Specialty == null ?
                    Query.Count() :
                    Query.Where(p => p.Sub == Specialty).Count()
            },
            _SortOrder = sortOrder,
            _CurrentCategory = Specialty
        };


        return View(model);
    }

我还必须创建一个HTML Helper类来进行排序,同时保持页码。仍在工作,但现在我实际上正在取得进展,谢谢!

1 个答案:

答案 0 :(得分:1)

OrderBy函数的工作方式是它需要一个lambda表达式,它返回一个对象的属性,并且你试图抛出一个字符串。做你想做的事情的一种方法是重新构造你的代码:(这是未经测试但应该给你的想法):

 var query = repository.Entries
                       .Where(p => Specialty == null || p.Sub == Specialty);

 switch (sortOrder)
 {
    case "Case":
        query = query.OrderBy(p => p.Case);
        break;

    case "Timestamp":
        query = query.OrderBy(p => p.TimeStamp);
        break;

    case "Origin":
        query = query.OrderBy(p => p.Origin);
        break;
 **SNIP**

var Entry = query.Skip((page - 1) * pagesize)
                  .Take(pagesize)
                  .ToList() //You probably need this here

如果您认为此查询效率低,请不要担心,由于延迟执行,在您尝试使用ToList()实现结果之前,数据库实际上不会是查询