动态模型排序

本文关键字:排序 模型 动态 | 更新日期: 2023-09-27 18:00:31

所以无可否认,我是MVC的新手,在这方面我遇到了很多困难。

我的DB表由入口控制器中的实体框架的IEntryRepository实例化。我可以提取数据,我的分页助手似乎正在工作。我甚至可以过滤结果。但是我很难弄清楚如何对列进行排序。

来自控制器:

   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>

我知道这里面有很多错误,但我的主要问题是为什么会这样。不接受我传递给它的任何东西?这是一种不恰当的使用Lambda的方式吗?否则怎么办?

我找到了不少教程,但似乎都没有涉及到对实体框架模型进行排序。

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

EDIT(已修复):

控制器:

    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);
    }

我还必须创建一个HTMLHelper类来进行排序,同时维护页码。还在工作,但现在我确实在进步,谢谢!

动态模型排序

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() 实现结果之前,数据库实际上不会是查询