使用外键从项目列表中进行选择

时间:2017-02-04 01:46:35

标签: c# mysql asp.net-mvc asp.net-mvc-5

我制作音乐数据库,当我选择一位艺术家时,我想要一张他们的专辑列表。因此,我只需要选择外部ID与艺术家ID(Album.ArtistID == Artist.ID)匹配的项目,以显示在视图中。我该怎么做?此外,如果您可以为学习目的解释原因。

控制器中的方法:

express-enforces-ssl

观点:

public ActionResult AlbumIndex(int? id)
        {
            return View(db.Albums.ToList());
        }

public class Artist
    {
        public int ID { get; set; }
        public string Name { get; set; }
        [DataType(DataType.ImageUrl)]
        public string Picture { get; set; }
        public string Info { get; set; }

        public virtual ICollection<Song> Songs { get; set; }
        public virtual ICollection<Album> Albums { get; set; }
        public virtual ICollection<Genre> Genres { get; set; }
    }

public class Album
    {
        public int ID { get; set; }
        [ForeignKey("Artist")]
        public int ArtistID { get; set; }
        public string Name { get; set; }
        [Display(Name = "Release Year")]
        public string ReleaseYear { get; set; }
        [DataType(DataType.ImageUrl)]
        public string Picture { get; set; }
        public decimal Price { get; set; }

        public virtual Artist Artist { get; set; }
        public virtual ICollection<Song> Songs { get; set; }
        public virtual ICollection<Genre> Genres { get; set; }
    }

0 个答案:

没有答案