您好我试图制作音乐应用程序以共享音乐,但我有以下问题。 Look red circle
我放置了红色圆圈,我想加载我在课堂上制作的艺术家。我尝试使用字典但不使用扩展名PagedList
。
上面的网页视图如下所示:
<div class="panel panel-default">
<table class="table">
@foreach (var item in (IEnumerable<Song>) ViewData["songs"])
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
<a class="artist">Artiest</a>
</td>
<td>
<a class="playbtn btn btn-sm btn-default btn-circle pull-right" data-item="@item.Mp3File"><span class="glyphicon glyphicon-play"></span></a>
</td>
<td>
<button class="btn btn-default glyphicon glyphicon-plus favoritebutton pull-right">Add to favorites</button>
</td>
</tr>
}
</table>
歌曲的控制器如下所示:
public ActionResult Index(int page = 1, int pagesize = 8)
{
List<Artist> artists = artistRepository.GetArtistBySongID(int id);
List<Song> songs = songRepository.ReadSongs();
List<Song> recommended = songRepository.GetFavoritesById(Convert.ToInt32(Session["userid"]));
ViewData["recommended"] = recommended;
PagedList<Song> pagedList = new PagedList<Song>(songs, page, pagesize);
ViewData["songs"] = pagedList;
return View();
}
我的歌曲课程如下:
public class Song
{
private int id;
private string name;
private string genre;
private DateTime release_date;
private string mp3file;
public int Id
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Genre
{
get { return genre; }
set { genre = value; }
}
public DateTime ReleaseDate
{
get { return release_date; }
set { release_date = value; }
}
public string Mp3File
{
get { return mp3file; }
set { mp3file = value; }
}
public Song(string name, string genre, DateTime release_date, string mp3file)
{
this.name = name;
this.genre = genre;
this.release_date = release_date;
this.mp3file = mp3file;
}
public Song(int id, string name, string genre, DateTime release_date, string mp3file)
{
this.id = id;
this.name = name;
this.genre = genre;
this.release_date = release_date;
this.mp3file = mp3file;
}
public override string ToString()
{
return String.Format("{0}, {1}, {2}, {3}, {4}", Id, Name, Genre, ReleaseDate, mp3file);
}
答案 0 :(得分:0)
列出艺术家= artistRepository.GetArtistBySongID(int id);
这是你的第一个问题。你想要一个艺术家列表 - 每个都对应一首歌,但你只给它一首歌的id。这将返回一个艺术家,而不是一个列表。所以,你需要通过你的歌曲列表,拉出id,获得该歌曲的艺术家,并建立一个列表。
import (
"fmt"
"net/smtp"
"strings"
)
type Mail struct {
from string
to []string
subject string
body string
}
type SMTPServer struct {
host string
port string
}
func (s *SMTPServer) serverName() string {
return s.host + ":" + s.port
}
func (mail *Mail) buildMessage() string {
message := ""
message += fmt.Sprintf("From: %s\r\n", mail.from)
if len(mail.to) > 0 {
message += fmt.Sprintf("To: %s\r\n", strings.Join(mail.to, ";"))
}
message += fmt.Sprintf("Subject: %s\r\n", mail.subject)
message += "\r\n" + mail.body
return message
}
func Send(mail Mail, server SMTPServer) (err error) {
// Connect to SMTP server
c, err := smtp.Dial(server.serverName())
if err != nil {
return
}
// Set the sender and recipients
if err = c.Mail(mail.from); err != nil {
return
}
for _, t := range mail.to {
if err = c.Rcpt(t); err != nil {
return
}
}
// Send the email body.
wc, err := c.Data()
if err != nil {
return
}
if _, err = wc.Write([]byte(mail.buildMessage())); err != nil {
return
}
if err = wc.Close(); err != nil {
return
}
if err = c.Quit(); err != nil {
fmt.Printf("%s", err)
return
}
return nil
}
func main() {
mail := Mail{"a@test.com", []string {"b@test.com", "c@test.com"}, "subject", "body"}
server := SMTPServer{"host", "port"}
err := Send(mail, server)
fmt.Printf("%s", err)
}
但是,这只能解决部分问题,因为现在在视图中,我们必须在逐步浏览歌曲列表的同时逐步浏览艺术家列表。
我认为最简单的解决方案是向List<Artist> artists = new List<Artist>();
List<Song> songs = songRepository.ReadSongs();
PagedList<Song> pagedList = new PagedList<Song>(songs, page, pagesize);
foreach(var song in pagedList)
artists.Add(artistRepository.GetArtistBySongID(song.ID));
ViewData["songs"] = pagedList;
ViewData["artists"] = artists;
对象添加Artist
属性,但这可能会导致问题。 (但是,在继续之前先尝试一下)。
如果你不能将属性添加到SOng对象,那么接下来最好是制作一个元组。
Song
然后在你看来改变
List<Song> songs = songRepository.ReadSongs();
PagedList<Song> pagedList = new PagedList<Song>(songs, page, pagesize);
var songartists = new List<Tuple<Song, Artist>>
foreach(var song in pagedList)
songartists .Add(Tuple.Creeate(song, artistRepository.GetArtistBySongID(song.ID)));
ViewData["songs"] = songartists ;
为:
@foreach (var item in (IEnumerable<Song>) ViewData["songs"])
{