title: MVC dropdownlist with values from the database
body: <p>Based on <a href="https://mvcmusicstore.codeplex.com/" rel="nofollow noreferrer">https://mvcmusicstore.codeplex.com/</a> trying to get a drop down list of attributes in the Details album before buying. I created a table with a map of attributes and table with attributes. In principle, the product has to refer to the map of attributes, which is to define what akrybuty will appear in the drop-down list. enter image description hereSomeone can help me to implement in the application?</p>
  
  <p>I know that in the view just add </p>
  
  <pre><code>@Html.DropDownList("Name",new SelectList(ViewBag.Names))
  </code></pre>
  
  <p>, I do not know how to get the data from that map the attributes so that each album had its own set of attributes from all available.</p>
  
  <p>Controller:</p>
  
  <pre><code>namespace MvcMusicStore.Controllers
  {
      public class StoreController : Controller
      {
          MusicStoreEntities storeDB = new MusicStoreEntities();
  
          //
          // GET: /Store/
  
          public ActionResult Index()
          {
              var genres = storeDB.Genres.ToList();
  
              return View(genres);
          }
  
  
          public ActionResult Browse(string genre)
          {
              // Retrieve Genre and its Associated Albums from database
              var genreModel = storeDB.Genres.Include("Albums")
                  .Single(g =&gt; g.Name == genre);
  
              return View(genreModel);
          }
  
  
          public ActionResult Details(int id)
          {
              var album = storeDB.Albums.Find(id);
  
              return View(album);
          }        
      }
  }
  </code></pre>
  
  <p>Models:</p>
  
  <h1>1</h1>
  
  <pre><code>namespace MvcMusicStore.Models
  {
      [Bind(Exclude = "AlbumId")]
      public class Album
      {
          [ScaffoldColumn(false)]
          public int AlbumId { get; set; }
  
          [DisplayName("Genre")]
          public int GenreId { get; set; }
  
          [DisplayName("Artist")]
          public int ArtistId { get; set; }
  
          [Required(ErrorMessage = "An Album Title is required")]
          [StringLength(160)]
          public string Title { get; set; }
  
          [Required(ErrorMessage = "Price is required")]
          [Range(0.01, 100.00,
              ErrorMessage = "Price must be between 0.01 and 100.00")]
          public decimal Price { get; set; }
  
          [DisplayName("Album Art URL")]
          [StringLength(1024)]
          public string AlbumArtUrl { get; set; }
  
          public virtual Genre Genre { get; set; }
          public virtual Artist Artist { get; set; }
          public virtual List&lt;OrderDetail&gt; OrderDetails { get; set; }
      }
  }
  </code></pre>
  
  <h1>2</h1>
  
  <pre><code>namespace MvcMusicStore.Models
  {
      public class Artist
      {
          public int ArtistId { get; set; }
          public string Name { get; set; }
      }
  }
  </code></pre>
  
  <h1>3</h1>
  
  <pre><code>namespace MvcMusicStore.Models
  {
      public partial class Genre
      {
          public int GenreId { get; set; }
          public string Name { get; set; }
          public string Description { get; set; }
          public List&lt;Album&gt; Albums { get; set; }
      }
  }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>@model MvcMusicStore.Models.Album
  
  @{
  ViewBag.Title = "Album - " + Model.Title;
  }
  
  &lt;h2&gt;@Model.Title&lt;/h2&gt;
  
  &lt;p&gt;
  &lt;img alt="@Model.Title" src="@Model.AlbumArtUrl" /&gt;
  &lt;/p&gt;
  
   &lt;div id="album-details"&gt;
  &lt;p&gt;
      &lt;em&gt;Genre:&lt;/em&gt;
      @Model.Genre.Name
  &lt;/p&gt;
  &lt;p&gt;
      &lt;em&gt;Artist:&lt;/em&gt;
      @Model.Artist.Name
  &lt;/p&gt;
  &lt;p&gt;
      &lt;em&gt;Price:&lt;/em&gt;
      @String.Format("{0:F}", Model.Price)
  &lt;/p&gt;
  &lt;p class="button"&gt;
      @Html.ActionLink("Add to cart", "AddToCart", 
      "ShoppingCart", new { id = Model.AlbumId }, "")
  &lt;/p&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>the code I have currently.</p>
  
  <p>I want to add dropdownlisty given the fact that each product will have different attributes such as one will have 8 types of cover, while the other only two. When you select each item (each with 8 covers) dropdownlist below it will zmianiała its contents</p>
  
  <p>Picture an example of what he wants to achieve.</p>
  
  <p><a href="https://i.stack.imgur.com/IIApj.png" rel="nofollow noreferrer">https://i.stack.imgur.com/IIApj.png</a></p>