在我的asp mvc 3應用程序中,我想顯示一張照片專輯。所以會有選定的圖片和相冊中其他圖片的縮略圖列表。用戶將能夠看到的縮略圖列表僅包含8張照片,其他所有照片都將被隱藏。我想要的是從選定的項目開始這個列表,當列表到達最後而沒有完成所有項目時,它將從頭開始重新開始。如何用C#列表製作一個循環#
我設法做到這一點,但我覺得它很快,很髒。是否有任何內置的C#函數可以用來做到這一點?
@{int i = 0;}
@foreach (AlbumPhoto albmphoto in Model.AlbumPhotoList
.Where(p => p.AlbumPhotoId > int.Parse(SinglePhoto))
.OrderBy(p => p.AlbumPhotoId))
{
i++;
string show = "none";
if (i < 8)
{
show = "block";
}
<a href="#" style="display: @show">
<img src="@Url.Content(albmphoto.AlbumPhotoPath)" width="70" height="47" border="0" alt="@albmphoto.AlbumPhotoDescription" />
</a>
}
@foreach (AlbumPhoto albmphoto in Model.AlbumPhotoList
.Where(p => p.AlbumPhotoId < int.Parse(SinglePhoto))
.OrderBy(p => p.AlbumPhotoId))
{
i++;
string show = "none";
if (i < 8)
{
show = "block";
}
<a href="#" style="display: @show">
<img src="@Url.Content(albmphoto.AlbumPhotoPath)" width="70" height="47" border="0" alt="@albmphoto.AlbumPhotoDescription" />
</a>
}
感謝您的答覆,我做了像你的第一個代碼,但使用了一個工會,而不是。我將列表分成2個列表:第一個列表包含位於起始索引之後的元素,另一個列表包含其餘元素。然後在foreach循環中使用:foreach(var albumphoto in photosAfter.Union(photosBefore)) – kbaccouche