2015-11-30 112 views
1

我想按作者姓氏,出版日期(最新和最舊),標題,最受歡迎和最高評分對我的搜索結果進行排序!排序/篩選MVC

我用了一個n例子,幾乎將它逐字複製,但它對我來說不起作用......任何關於我在做什麼的錯誤都是錯誤的?

這裏是我的控制器代碼:

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

什麼不起作用?你收到任何錯誤?也許你錯誤地複製了「most_popular」,因爲它具有與「highest_rated」相同的邏輯,應該使用BookPopularity來代替。 –

+0

它只是不排序!當我點擊超鏈接時沒有任何反應。我沒有收到任何錯誤:(( –

+0

您是否啓動調試器? – moji

回答

0

Index()剛剛返回未分類收集(獲取集合,然後分類整理後,你折騰它會再次使用return View(db.Books.ToList());再次調用數據庫,這將返回未分類的集合。

更改最後一行您的代碼爲

return View(books); 
+0

這工作,非常感謝! –