8

I'm new to ASP.NET MVC. I'm trying to figure out how create a basic drop down list from values in my database. In ASP.NET web forms, I know I can load a drop down list like this:

Page.aspx

<asp:DropDownList ID="myDropDownList" runat="server" DataTextField="FullName" DataValueField="ID" OnLoad="myDropDownList_Load" />

Page.aspx.cs

void myDropDownList_Load(object sender, EventArgs e)
{
  if (Page.IsPostBack == false)
  {
    List<Person> people = GetPeopleFromDatabase();
    myDropDownList.DataSource = people;
    myDropDownList.DataBind();
  }
}

How do I do the same type of thing in ASP.NET MVC? Thank you!

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
user70192
  • 13,786
  • 51
  • 160
  • 240
  • Take a look at this blog post that explains it all: [Drop-down Lists and ASP.NET MVC](http://odetocode.com/Blogs/scott/archive/2010/01/18/drop-down-lists-and-asp-net-mvc.aspx) – Leniel Maccaferri Mar 07 '10 at 17:09
  • as @LenielMacaferi sugests SelectList is your friend create one in you view's model. – kenny Jul 15 '12 at 14:59

2 Answers2

8

Model

public class EditSongViewModel
{        
    public int AlbumId { get; set; }
    public string Title { get; set; }                
    public int TrackNumber { get; set; }
    public IEnumerable<SelectListItem> Albums { get; set; }
}

Extension method

public static IEnumerable<SelectListItem> ToSelectListItems(
              this IEnumerable<Album> albums, int selectedId)
{
    return 
        albums.OrderBy(album => album.Name)
              .Select(album => 
                  new SelectListItem
                  {
                    Selected = (album.ID == selectedId),
                    Text = album.Name,
                    Value = album.ID.ToString()
                   });
}

Getting data from database

model.Albums = _repository.FindAllAlbums().ToSelectItems(selectedId);

View

@Html.DropDownList("AlbumId", Model.Albums)

or better yet:

@Html.DropDownListFor(model => model.AlbumId, Model.Albums)

Take a look at this blog post that explains it all:

Drop-down Lists and ASP.NET MVC

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • where does model.AlbumId come from in this example? I don't see 'AlbumId' defined on your model – dferraro Apr 15 '14 at 22:26
  • @dferraro great... you found an error! Just opened the link in the bottom of my answer AGAIN and saw that the author added `AlbumId` to the `EditSongViewModel`. When I added my answer 3 years ago that thingy was not in the model and of course it should be. :D – Leniel Maccaferri Apr 16 '14 at 02:08
  • @Lenial Macaferi thank you for clarifying. And what does the 'AlbumId' represent here? Does it always represent the 'selected AlbumId'? – dferraro Apr 16 '14 at 14:16
  • @dferraro: Exactly. It's the backing field that holds the selected AlbumId from the dropdown. – Leniel Maccaferri Apr 17 '14 at 01:13
  • 1
    Is it a good idea to use UI classes like SelectListItem in the Controller? – Legends Mar 25 '16 at 13:07
2

In MVC2, use <%=Html.DropListFor(x => x.MemberName, Model.DropListItems)%> in your view and in your controller you populate DropListItems with a new SelectList containing the items from the database.

I belive that the Nerd Dinner-sample includes this, and if you're new to MVC you should really really go through and create the Nerd Dinner app, because you learn so much from it, even if you plan to not use what they use.

Svante Svenson
  • 12,315
  • 4
  • 41
  • 45