0

Background - I'm trying to populate a drop down list with state information from a database. I'd like the full state name to be the option and the state abbreviation to be the value. Example:

<option value="AL">ALABAMA</option>

Current Progress - The full state names and abbreviations already exist in the DB. I've successfully populated the DDL with full state names from the DB. Here's the code I've used to do this (minus stuff I've deemed irrelevant).

Model Context (generated from template):

Partial Public Class BrokerCRMEntities
    Public Property States() As DbSet(Of State)
End Class

State Model (generated from template):

Partial Public Class State
    Public Property StateAbbrev As String
    Public Property StateFull As String
End Class

Controller:

Dim db As BrokerCRMEntities = New BrokerCRMEntities
Dim StateList = New List(Of String)()
Dim StateQuery = From d In db.States
                 Order By d.StateFull
                 Select d.StateFull
StateList.AddRange(StateQuery)
ViewBag.State = New SelectList(StateList)

View:

@ModelType IEnumerable(Of BrokerCRM.BrokerCRMEntities)
@Html.DropDownList("State", "")

This code produces a DDL which contains full state names. Example:

<option>ALABAMA</option>

Question - In addition to populating full state names like I've done above, I'd also like to populate the value of the select options in the same DDL with the state abbreviations in my DB/model. How is this done?

Thanks!

Justin
  • 3
  • 2
  • Don't have a VB handle handy, but in C# you could change the "select" part of the query to Select New SelectListItem {Text = d.StateFull, Value = d.StateAbbreviation}. Then you have a list of SelectListItems and you can bind the dropdown directly to that. – Tridus Jun 07 '11 at 22:07

1 Answers1

0

I would recommend you using a view model along with a strongly typed view and DropDownListFor helper. So as always start with the view model:

Public Class StatesViewModel
    Public Property SelectedState As String
    Public Property States As IEnumerable(Of State)
End Class

then the controller:

Public Function Index() As ActionResult
    ' TODO: use ctor DI of the repository
    Dim db = New BrokerCRMEntities()
    Dim model = New StatesViewModel() With { _
        Key .States = db.States.OrderBy(Function(x) x.StateFull) _
    }
    Return View(model)
End Function

and finally the view:

@ModelType IEnumerable(Of StatesViewModel)
@Html.DropDownListFor(
    Function(x) x.SelectedState,
    New SelectList(Model.States, "StateAbbrev", "StateFull")
)
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • My current view contains two DDLs (States, Programs) that both need to be populated in the same manner. Programs is also included in the Context/Model and ready for querying just like States. Is using a strongly typed view still the best approach? Thanks for your patience. I'm new to all of this. – Justin Jun 08 '11 at 11:52
  • **Answer** - I used this approach. In order to populate multiple DDLs this way in the same view, I simply added properties to the ViewModel for each DDL and set those properties with additional Linq queries in the controller. Thank you! – Justin Jun 08 '11 at 15:30