2011-08-18 59 views
0

我已經從教程中創建了MVC音樂商店,並且正在編輯它並添加新功能,最近我讓瀏覽商店頁面在最後帶有從A-Z到All按鈕的鏈接。 如何提示「沒有藝術家以字母A,B,C開頭」或任何字母沒有藝人點擊的消息。MVC過濾A-Z

這裏是我的存儲索引我的代碼:

@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>(); 
     } 

回答

1
@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> 
} 
+0

這就是現在所有排序。謝謝 – JaredH20

2

爲了使您的視圖更簡潔,你可以使用:

@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 }); 
} 

這將節省所有這些acti鏈接