0

What is the correct way to populate a SelectList when using the Html.DropDownListFor helper in ASP.Net MVC 3?

I basically have a "Create" form which I want to specify the entry values on for a particular field. For example, it is a movie database and I want the ability to enter a Genre, but I don't want users to be able to enter what they like, so I assume a SelectList to be the best answer?

A way that works is to create a new class with a static method to return the SelectList:

public class Genres
{
    public static List<string> GetGenres()
    {
        List<string> myGenres = new List<string>();
        myGenres.Add("Comedy");
        myGenres.Add("Romantic Comedy");
        myGenres.Add("Sci-Fi");
        myGenres.Add("Horror");

        return myGenres;
    }
}

Which can then be used like this:

    <div class="editor-field">
        @Html.DropDownListFor(model => model.Genre, new SelectList(OLL.Models.Genres.GetGenres()))
    </div>

It just doesn't seem like the right/easiest way to do it? I think I'm missing something obvious here...

Thanks for any assistance you can provide.

3
  • This looks like a fine solution to me. Perhaps you could factor out the new SelectList code into another method, but I don't see a problem with your current code. Commented May 4, 2011 at 15:15
  • I think I'm going to create a generic model class which then has static IEnumerables for each field I need to do this with. For example, public static IEnumerable<string> Genres = new List<string> { "Action", "Adventure", "Animation" }; Commented May 4, 2011 at 15:50
  • Perhaps this thread will help.. stackoverflow.com/questions/8740419/… Commented May 29, 2018 at 19:09

2 Answers 2

1

This is a simple selectList style I use for drop downs all the time, hopefully you find this helpeful.

 ViewBag.AlbumListDD = from c in db.Query<DatabaseEntities.VenuePhotoAlbum>("WHERE VenueId = @0", VenueId)
                      select new SelectListItem
                      {
                         Text = c.Name,
                         Value = c.Id.ToString()
                      };

Inside my view:

@Html.DropDownList("AlbumId", (IEnumerable<SelectListItem>)ViewBag.AlbumListDD, new { id = "ChangeAlbum" })

Hopefully you can find this useful as this is a database route to go. I typically like to have all of that sort of information in a database and that way users can dynamically add/remove and change these dropdown items at will. There is a bit of SQL work behind the scenes required when removing a type but it has proven to be very successful and fast for me at this point.

Sign up to request clarification or add additional context in comments.

Comments

0
 var og = (from s in DB.Products
 select new SelectListItem
  {
        Text = s.ProductName,
         Value = s.ProductID.ToString()
  }); 
  ViewBag.lstProducts = new SelectList(getproductname, "ProductID", "ProductName");
<p>
    @Html.DropDownList("AlbumId", (IEnumerable<SelectListItem>)ViewBag.lstProducts, new { @id = "drp" });
</p>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.