排序/过滤MVC [英] Sorting/Filtering MVC

查看:83
本文介绍了排序/过滤MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to sort my search results by author last name, publication date (newest and oldest), title, most popular, and highest rated!

我用了一个n例子,几乎逐字复制了它,但是对我来说不起作用...关于我做错了什么建议?

I used a n example and almost copied it verbatim, but it will not work for me...any suggestions on what I am doing wrong?

这是我的控制器代码:

  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>

推荐答案

您的Index()只是返回未排序的集合(获取该集合然后对其进行排序后,您将其扔掉并使用return View(db.Books.ToList());再次调用数据库返回未排序的集合.

Your Index() is just returning the unsorted collection (after getting the collection and then sorting it, you toss it away and call the database again using return View(db.Books.ToList()); which returns an unsorted collection.

将代码的最后一行更改为

Change the last line of your code to

return View(books);

这篇关于排序/过滤MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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