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

Posted by Kelsey on Stack Overflow See other posts from Stack Overflow or by Kelsey
Published on 2010-03-07T22:39:45Z Indexed on 2010/03/07 23:11 UTC
Read the original article Hit count: 649

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?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about asp.net-mvc