Skip to content

Pet List

June 17, 2011

Here are snippets of code which I found really useful when setting a drop down list element using HtmlHelpers in MVC 2 to a ViewData to be displayed on the view pages.

Dim petList As List(Of String) = New List(Of String)
petList.Add(“Dog”)
petList.Add(“Cat”)
petList.Add(“Hamster”)
petList.Add(“Parrot”)
petList.Add(“Gold fish”)
petList.Add(“Mountain lion”)
petList.Add(“Elephant”)

DialogScreenView.ViewData(“pets”) = New SelectList(petList)

or…..

Dim petList As List(Of String) = New List(Of String)

For Each petName In oPetList.PetList
    petList.Add(petName.Text)
Next

DialogScreenView.ViewData(“pets”) = New SelectList(petList)

DialogScreenView.ViewData(“Screen”) = TempData(“Screen”)

or….

Dim petList As New List(Of SelectListItem)()

For Each petName In oPetList.PetList
    Dim petItem As SelectListItem = New SelectListItem
    petItem.Text = petName.Text
    petItem.Value = petName.Value
    petList.Add(petItem)
Next

DialogScreenView.ViewData(“pets”) = petList

DialogScreenView.ViewData(“Screen”) = TempData(“Screen”)

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: