[英]MVC Filtering A-Z
我已經從教程中制作了MVC音樂商店,並對其進行了編輯和添加了新功能,最近,我使瀏覽商店頁面具有來自AZ的鏈接,最后帶有一個“全部”按鈕。 如何顯示“沒有以字母A,B,C開頭的藝術家”或沒有藝術家的字母被單擊的消息。
這是我的商店索引中的代碼:
@model IEnumerable<MVCMusicStore.Models.Artist>
@{
ViewBag.Title = "Store";
}
<h3>Browse Artists</h3>
@Html.ActionLink("A", "Index", new { letter = "A" })
@Html.ActionLink("B", "Index", new { letter = "B" })
@Html.ActionLink("C", "Index", new { letter = "C" })
@Html.ActionLink("D", "Index", new { letter = "D" })
@Html.ActionLink("E", "Index", new { letter = "E" })
@Html.ActionLink("F", "Index", new { letter = "F" })
@Html.ActionLink("G", "Index", new { letter = "G" })
@Html.ActionLink("H", "Index", new { letter = "H" })
@Html.ActionLink("I", "Index", new { letter = "I" })
@Html.ActionLink("J", "Index", new { letter = "J" })
@Html.ActionLink("K", "Index", new { letter = "K" })
@Html.ActionLink("L", "Index", new { letter = "L" })
@Html.ActionLink("M", "Index", new { letter = "M" })
@Html.ActionLink("N", "Index", new { letter = "N" })
@Html.ActionLink("O", "Index", new { letter = "O" })
@Html.ActionLink("P", "Index", new { letter = "P" })
@Html.ActionLink("Q", "Index", new { letter = "Q" })
@Html.ActionLink("R", "Index", new { letter = "R" })
@Html.ActionLink("S", "Index", new { letter = "S" })
@Html.ActionLink("T", "Index", new { letter = "T" })
@Html.ActionLink("U", "Index", new { letter = "U" })
@Html.ActionLink("V", "Index", new { letter = "V" })
@Html.ActionLink("W", "Index", new { letter = "W" })
@Html.ActionLink("X", "Index", new { letter = "X" })
@Html.ActionLink("Y", "Index", new { letter = "Y" })
@Html.ActionLink("Z", "Index", new { letter = "Z" })
@Html.ActionLink("All", "Index", new { letter = "all" })
<ul>
@foreach (var artist in Model)
{
<li>@Html.ActionLink(artist.Name,
"Browse", new { id = artist.ArtistId })</li>
}
</ul>
這是我的控制器中的代碼:
public ActionResult Index(string letter = "")
{
IEnumerable<Artist> artist;
if (letter == "all")
{
artist = storeDB.Artists.OrderBy(x =>x.Name).ToList();
}
else if (letter != "")
{
artist = storeDB.Artists.Where(a => a.Name.StartsWith(letter)).OrderBy(x => x.Name).ToList();
}
else
{
artist = new List<Artist>();
}
要使視圖更整潔,可以使用:
@for (int y = 0; y < 27; y++) {
char filter = Convert.ToChar(65 + y);
string letter = filter.toString();
if(y == 27){
letter = "All";
}
@Html.ActionLink(letter, "Index", new { letter = letter });
}
這將保存所有這些動作鏈接
@if(Model.Any())
{
foreach (var artist in Model)
{
<li>@Html.ActionLink(artist.Name,
"Browse", new { id = artist.ArtistId })</li>
}
}
else
{
<span>There are no artists</span>
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.