2011-08-18 3 views
0

튜토리얼에서 MVC 뮤직 스토어를 만들고 편집하고 새로운 기능을 추가하고 있습니다. 최근에 Browse 스토어 페이지에 A-Z 링크가 있고 끝에는 모두 버튼이 있습니다. "편지 A, B, C로 시작하는 아티스트가 없습니다"또는 아티스트없이 클릭 된 글자를 가져 오는 방법은 무엇입니까? 당신이 사용할 수있는 깔끔한MVC 필터링 A-Z

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

이보기를 만들려면 :

이 내 컨트롤러에서 코드가

@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> 

: 여기

내 상점 색인에서 내 코드입니다
@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 링크에

관련 문제