views:

930

answers:

5

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?

+1  A: 

(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.

svinto
A: 

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

Drop-down Lists and ASP.NET MVC

Leniel Macaferi
+1  A: 

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.

CRice
+6  A: 

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.

Rob Conery
The trouble with using ViewData is that it makes your controller so much harder to test. If you keep everything in the ViewModel your tests are much better.
Mac
It's actually no different at all - with the ViewModel you have to spelunk ViewData.Model, otherwise you just ask for it with a key. No difference at all...
Rob Conery
A: 

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()
                             }); 
James H