Simple Dropdown List Example in VB.NET

VB.NET and creating dropdown menus are perhaps the two most annoying aspects of the .NET framework, and a close third is trying to track down .NET information that isn’t written in C# or populated with database queries. What if you want an incredibly simple representation of an HTML dropdown menu that is handled through Razor’s great error checking?

In your controller, create a function that will generate the values that the dropdown will be populated with;

        Private Shared Function GenerateStatuses() As List(Of SelectListItem)
            Dim statuses = New List(Of SelectListItem)
            statuses.Add(New SelectListItem With {.Text = "---", .Value = "---"})
            statuses.Add(New SelectListItem With {.Text = "Planning", .Value = "1"})
            statuses.Add(New SelectListItem With {.Text = "Closed", .Value = "2"})
            statuses.Add(New SelectListItem With {.Text = "Cancelled", .Value = "3"})
            Return statuses
        End Function

The list will be of SelectListItem, which is a little class that has text and value pairs that will populate the dropdown menu.
Each StatusListItem is added to the list in order with the aptly named “Add” function.

        Private Shared Function GenerateMonths() As List(Of SelectListItem)
            Dim months = ( _
                From p In Enumerable.Range(1, 12) _
                Select New SelectListItem With {.Text = p.ToString(), .Value = p.ToString()})
            Dim returnmonths = New List(Of SelectListItem)
            returnmonths = months.ToList()
            'crams the value into the beginning of the list
            returnmonths.Insert(0, New SelectListItem With {.Text = "---", .Value = "---"})
            Return returnmonths
        End Function

If you have a large number of things to add, consider using an enumerable. In this case, we add the numbers 1 through 12 as both the text displayed and the value. Next, we change that enumerable into a list which gets saved as “returnmonths” instead of using “Add” again, we use “Insert” and choose the insertion point, which in this case is the very beginning of the list.

Still inside of your controller, call the function(s) that you created and pass them to the view. Notice that this example is using the stanky old “ViewData” instead of the stylish “ViewBag”. These examples are made under MVC2, but everything should work in MVC3.

        Function Index() As ActionResult
            ViewData("months") = GenerateMonths()
            ViewData("statuses") = GenerateStatuses()
            Return View()
        End Function

dropdown_example

Here’s the lackluster result from so many lines of code. Goddamn you VB.net

Finally, in your view, you would create the dropdowns using “Html.DropDownListFor”, and pass them the lists that you made back in the controller. After that, you will likely wonder why you are using VB.net instead of C#.

Leave a Reply

Your email address will not be published. Required fields are marked *