ASP.NET MVC 3 直到我膝盖中了一箭【14】MVC3MusicStore搜索
1.~/Views/StoreManager/下更新视图Index
1 @model PagedList.IPagedList<MVC3MusicStore.Models.Album> 2 @helper Truncate(string input, int length) 3 { 4 if (input.Length <= length) 5 { 6 @input 7 } 8 else 9 { 10 @input.Substring(0, length)<text>...</text> 11 12 } 13 } 14 @{ 15 ViewBag.Title = "Index"; 16 } 17 @using MVC3MusicStore.Helpers 18 <h2> 19 Index</h2> 20 <p> 21 @Html.ActionLink((string)ViewBag.CreateLink, "Create") 22 </p> 23 <p> 24 Show: 25 @if (ViewBag.CurrentFilter != "") 26 { 27 @Html.ActionLink("All", "Index", new 28 { 29 sortOrder = ViewBag.CurrentSortOrder, 30 Keyword = ViewBag.CurrentKeyword 31 }) 32 } 33 else 34 { 35 @:All 36 } 37 | 38 @if (ViewBag.CurrentFilter != "Less") 39 { 40 @Html.ActionLink("Less than 10", "Index", new 41 { 42 filter = "Less", 43 sortOrder = ViewBag.CurrentSortOrder, 44 Keyword = ViewBag.CurrentKeyword 45 }) 46 } 47 else 48 { 49 @:Less than 10 50 } 51 | 52 @if (ViewBag.CurrentFilter != "Greater") 53 { 54 @Html.ActionLink("Greater than 10", "Index", new 55 { 56 filter = "Greater", 57 sortOrder = ViewBag.CurrentSortOrder, 58 Keyword = ViewBag.CurrentKeyword 59 }) 60 } 61 else 62 { 63 @:Greater than 10 64 } 65 </p> 66 @using (Html.BeginForm()) 67 { 68 @:Search: @Html.TextBox("Keyword")<input type="submit" value="Search" /> 69 } 70 @Html.Partial("_Paging") 71 <table> 72 <tr> 73 <th> 74 @Html.ActionLink((string)ViewBag.GenreDisplay, "Index", new 75 { 76 sortOrder = ViewBag.GenreSortParam, 77 filter = ViewBag.CurrentFilter, 78 Keyword = ViewBag.CurrentKeyword 79 }) 80 </th> 81 <th> 82 @Html.ActionLink((string)ViewBag.ArtistDisplay, "Index", new 83 { 84 sortOrder = ViewBag.ArtistSortParam, 85 filter = ViewBag.CurrentFilter, 86 Keyword = ViewBag.CurrentKeyword 87 }) 88 </th> 89 <th> 90 @Html.ActionLink((string)ViewBag.TitleDisplay, "Index", new 91 { 92 sortOrder = ViewBag.TitleSortParam, 93 filter = ViewBag.CurrentFilter, 94 Keyword = ViewBag.CurrentKeyword 95 }) 96 </th> 97 <th> 98 @Html.ActionLink((string)ViewBag.PriceDisplay, "Index", new 99 {100 sortOrder = ViewBag.PriceSortParam,101 filter = ViewBag.CurrentFilter,102 Keyword = ViewBag.CurrentKeyword103 })104 </th>105 @*<th>106 AlbumArtUrl107 </th>*@108 <th>109 </th>110 </tr>111 @foreach (var item in Model)112 {113 <tr>114 <td>115 @*@item.GenreId*@116 @Html.DisplayFor(modelItem => item.Genre.Name)117 </td>118 <td>119 @*@item.ArtistId*@ @*@Html.DisplayFor(modelItem => item.Artist.Name)*@120 @Truncate(item.Artist.Name, 25)121 </td>122 <td>123 @*@item.Title*@124 @Truncate(item.Title, 25)125 </td>126 <td>127 @*@String.Format("{0:F}", item.Price)*@128 @Html.DisplayFor(modelItem => item.Price)129 </td>130 @* <td>131 @item.AlbumArtUrl132 </td>*@133 <td>134 @Html.ActionLink((string)ViewBag.EditLink, "Edit", new { id = item.AlbumId })|135 @Html.ActionLink((string)ViewBag.DetailsLink, "Details", new { id = item.AlbumId })|136 @Html.ActionLink((string)ViewBag.DeleteLink, "Delete", new { id = item.AlbumId })137 </td>138 </tr>139 }140 </table>141 @Html.Partial("_Paging")2.~/Views/Shared/下更新分部视图_Paging
- First
- Prev
- Next
- Last
3.~/Controllers/下更新控制器StoreManagerController
1 public ActionResult Index(string sortOrder, string filter,string Keyword, int page = 1) 2 { 3 //return View(); 4 //var albums = db.Albums.Include("Genre").Include("Artist"); 5 //return View(albums.ToList()); 6 7 #region ViewBag资源 8 ViewBag.CreateLink = Resource.CreateLink; 9 ViewBag.EditLink = Resource.EditLink;10 ViewBag.DetailsLink = Resource.DetailsLink;11 ViewBag.DeleteLink = Resource.DeleteLink;12 ViewBag.GenreDisplay = Resource.GenreDisplay;13 ViewBag.ArtistDisplay = Resource.ArtistDisplay;14 ViewBag.TitleDisplay = Resource.TitleDisplay;15 ViewBag.PriceDisplay = Resource.PriceDisplay;16 #endregion17 18 #region ViewBag排序PARAMS19 ViewBag.GenreSortParam = (sortOrder == "Genre.Name") ? "Genre.Name desc" : "Genre.Name";20 ViewBag.ArtistSortParam = (sortOrder == "Artist.Name") ? "Artist.Name desc" : "Artist.Name";21 ViewBag.TitleSortParam = (sortOrder == "Title") ? "Title desc" : "Title";22 ViewBag.PriceSortParam = (sortOrder == "Price") ? "Price desc" : "Price";23 24 //默认的排序顺序25 if (String.IsNullOrEmpty(sortOrder))26 {27 sortOrder = "Title desc";28 }29 30 ViewBag.CurrentSortOrder = sortOrder;31 #endregion32 33 #region 过滤器34 var albums = from a in db.Albums select a;35 switch (filter)36 {37 case "Less":38 albums = albums.Where(a => a.Price39 <= 1040 );41 break;42 case "Greater":43 albums = albums.Where(a => a.Price >44 10);45 break;46 default:47 // No filter needed48 break;49 }50 ViewBag.CurrentFilter =51 String.IsNullOrEmpty(filter) ? "" : filter;52 #endregion53 54 #region 关键字搜索55 if (!String.IsNullOrEmpty(Keyword))56 {57 albums = albums.Where(a =>58 a.Title.ToUpper().Contains(Keyword.ToUpper())59 || a.Title.ToUpper().Contains(60 Keyword.ToUpper()));61 }62 ViewBag.CurrentKeyword =63 String.IsNullOrEmpty(Keyword) ? "" : Keyword;64 #endregion65 66 //var albums = db.Albums.OrderBy(sortOrder);67 albums = albums.OrderBy(sortOrder);68 int maxRecords = 10;69 int currentPage = page;70 return View(albums.ToPagedList(currentPage, maxRecords));71 }TAG: