Best way of implementing DropDownList in ASP.NET MVC 2?

I am trying to understand the best way of implementing a DropDownList in ASP.NET MVC 2 using the DropDownListFor helper. This is a multi-part question.

First, what is the best way to pass the list data to the view?

  1. Pass the list in your model with a SelectList property that contains the data
  2. Pass the list in via ViewData

How do I get a blank value in the DropDownList? Should I build it into the SelectList when I am creating it or is there some other means to tell the helper to auto create an empty value?

Lastly, if for some reason there is a server side error and I need to redisplay the screen with the DropDownList, do I need to fetch the list values again to pass into the view model? This data is not maintained between posts (at least not when I pass it via my view model) so I was going to just fetch it again (it’s cached). Am I going about this correctly?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Your best bet is to create a SelectList in your Controller – use my extension method here:
http://blog.wekeroad.com/2010/01/20/my-favorite-helpers-for-aspnet-mvc

Pop that into ViewData using the same key as your property name:
ViewData["statusid"]=MySelectList

Then just use Html.DropDownFor(x=>x.StatusID) and you’re all set.

Method 2

Answering in parts:

  1. The best way IMHO is to pass the list in the ViewModel like this:
    public SelectList Colors
    {
        get
        {
            // Getting a list of Colors from the database for example...
            List<Color> colors = GetColors().ToList();
    
            // Returning a SelectList to be used on the View side
            return new SelectList(colors, "Value", "Name");
        }
    }
  2. To get a blank or default option like ( — Pick a color — ), you can do this on the view side:
    @Html.DropDownListFor(m => m.Color, Model.Colors, "-- Pick a color --")
  3. You’ll have to fetch/populate the list again if it’s part of the ViewModel.

Take a look at the following blog post. It can give you some tips:

Drop-down Lists and ASP.NET MVC

Method 3

You could do something like:

<%= Html.DropDownListFor((x => x.ListItems), Model.ListItems, "")%>

or
<%= Html.DropDownList("ListItems", Model.ListItems, "")%>

The last param ‘optionLabel’ makes a blank list item

In this case, you can see ListItems is a property of the model.

I have made the view strongly typed to the model also.

Method 4

(You know this already!)

  1. Pass the list in your model with a SelectList property that contains the data

Yes, add it when you build the SelectList. (If you build the list using LINQ, Union might come in handy.)

Yes do do, and yes you are.

Method 5

I find it more intuitive to work with a sequence of SelectListItems (rather than a SelectList).

For example, this would create an IEnumerable<SelectListItem> from a sequence of customer objects that you can pass to the Html.DropDownListFor(…) helper. The ‘Selected’ property will optionally set the default item in the dropdown list.

var customers = ... // Get Customers
var items = customers.Select(c => new SelectListItem
                             {
                                 Selected = (c.Id == selectedCustomerId),
                                 Text = c.Email,
                                 Value = c.Id.ToString()
                             });


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x