排序/过滤MVC

时间:2015-11-30 00:43:59

标签: asp.net-mvc sorting

我正在尝试按作者姓氏,出版日期(最新和最旧),标题,最受欢迎和评分最高的方式对搜索结果进行排序!

我使用了一个例子并且几乎一字不差地复制了它,但它对我不起作用......对我做错了什么建议?

这是我的控制器代码:

  public ActionResult Index(string sortOrder)
    {
        ViewBag.LastNameParam = String.IsNullOrEmpty(sortOrder) ? "name" : "";
        ViewBag.NewestDateParam = sortOrder == "Date" ? "date_desc" : "Date";
        ViewBag.OldestDateParam = sortOrder == "date_desc";
        ViewBag.TitleParam = sortOrder == "title";
        ViewBag.RatingParam = sortOrder == "highest_rated";
        ViewBag.MostPopularParam = sortOrder == "most_popular";

        var books = from b in db.Books
                    select b;

        switch (sortOrder)
        {
            case "name":
                books = books.OrderBy(s => s.BookAuthorLastName);
                break;
            case "title":
                books = books.OrderBy(s => s.BookTitle);
                break;
            case "date":
                books = books.OrderBy(s => s.BookPublicationDate);
                break;
            case "date_desc":
                books = books.OrderByDescending(s => s.BookPublicationDate);
                break;
            case "highest_rated":
                books = books.OrderBy(s => s.BookRating);
                break;
            case "most_popular":
                books = books.OrderBy(s => s.BookRating);
                break;  
            default:
                books = books.OrderBy(s => s.BookAuthorLastName);
                break;
        }

        return View(db.Books.ToList());
    }

这是我的观看代码:

    @model IEnumerable<FinalProject_Lio_Lopez_Jafri_Wood.Models.Book>
@using FinalProject_Lio_Lopez_Jafri_Wood.Models;

@{
    ViewBag.Title = "Index";
}

<h2>List of All Books</h2>

<div class="row">
    <table class="table table-hover">
        <thead>
            <tr>
                <th> @Html.ActionLink("Book Title", "Index", new { sortOrder = ViewBag.TitleParam })</th>
                <th>Author First Name</th>
                <th> @Html.ActionLink("Author Last Name", "Index", new {sortOrder = ViewBag.LastNameParam}) </th>
                <th> Unique Number</th>
                <th> Amount in Stock</th>
                <th> @Html.ActionLink("Rating", "Index", new { sortOrder = ViewBag.RatingParam }) </th>
                <th> @Html.ActionLink("Total # of Purchases", "Index", new { sortOrder = ViewBag.MostPopularParam }) </th>
                <th>@Html.ActionLink("Publication Date", "Index", new {sortOrder = ViewBag.NewestDateParam})</th>
            </tr>
        </thead>


        @foreach (var item in Model)
        {
            <tr>
                <td>@Html.ActionLink(item.BookTitle, "Details", new { id = item.BookID })</td>
                <td>@item.BookAuthorFirstName</td>
                <td>@item.BookAuthorLastName</td>
                <td>@item.BookID</td>
                <td>@item.BookAmountInStock</td>
                <td>@item.BookRating</td>
                <td>@item.BookPopularity</td>
                <td>@item.BookPublicationDate</td>
            </tr>
        }
        </table>
    </div>

1 个答案:

答案 0 :(得分:0)

你的Index()只是返回未排序的集合(在获取集合然后对其进行排序后,将其丢弃并使用return View(db.Books.ToList());再次调用数据库,这将返回未排序的集合。

将代码的最后一行更改为

return View(books);