SelectExtensions Class

Visual Studio 2010

Represents support for making selections in a list.

System.Object
  System.Web.Mvc.Html.SelectExtensions

Namespace:  System.Web.Mvc.Html
Assembly:  System.Web.Mvc (in System.Web.Mvc.dll)

'Declaration
<ExtensionAttribute> _
Public NotInheritable Class SelectExtensions

  NameDescription
Public methodStatic memberDropDownList(HtmlHelper, String)Returns a single-selection select element using the specified HTML helper and the name of the form field.
Public methodStatic memberDropDownList(HtmlHelper, String, IEnumerable(Of SelectListItem))Returns a single-selection select element using the specified HTML helper, the name of the form field, and the specified list items.
Public methodStatic memberDropDownList(HtmlHelper, String, String)Returns a single-selection select element using the specified HTML helper, the name of the form field, and an option label.
Public methodStatic memberDropDownList(HtmlHelper, String, IEnumerable(Of SelectListItem), IDictionary(Of String, Object))Returns a single-selection select element using the specified HTML helper, the name of the form field, the specified list items, and the specified HTML attributes.
Public methodStatic memberDropDownList(HtmlHelper, String, IEnumerable(Of SelectListItem), Object)Returns a single-selection select element using the specified HTML helper, the name of the form field, the specified list items, and the specified HTML attributes.
Public methodStatic memberDropDownList(HtmlHelper, String, IEnumerable(Of SelectListItem), String)Returns a single-selection select element using the specified HTML helper, the name of the form field, the specified list items, and an option label.
Public methodStatic memberDropDownList(HtmlHelper, String, IEnumerable(Of SelectListItem), String, IDictionary(Of String, Object))Returns a single-selection select element using the specified HTML helper, the name of the form field, the specified list items, an option label, and the specified HTML attributes.
Public methodStatic memberDropDownList(HtmlHelper, String, IEnumerable(Of SelectListItem), String, Object)Returns a single-selection select element using the specified HTML helper, the name of the form field, the specified list items, an option label, and the specified HTML attributes.
Public methodStatic memberDropDownListFor(Of TModel, TProperty)(HtmlHelper(Of TModel), Expression(Of Func(Of TModel, TProperty)), IEnumerable(Of SelectListItem))Returns an HTML select element for each property in the object that is represented by the specified expression using the specified list items.
Public methodStatic memberDropDownListFor(Of TModel, TProperty)(HtmlHelper(Of TModel), Expression(Of Func(Of TModel, TProperty)), IEnumerable(Of SelectListItem), IDictionary(Of String, Object))Returns an HTML select element for each property in the object that is represented by the specified expression using the specified list items and HTML attributes.
Public methodStatic memberDropDownListFor(Of TModel, TProperty)(HtmlHelper(Of TModel), Expression(Of Func(Of TModel, TProperty)), IEnumerable(Of SelectListItem), Object)Returns an HTML select element for each property in the object that is represented by the specified expression using the specified list items and HTML attributes.
Public methodStatic memberDropDownListFor(Of TModel, TProperty)(HtmlHelper(Of TModel), Expression(Of Func(Of TModel, TProperty)), IEnumerable(Of SelectListItem), String)Returns an HTML select element for each property in the object that is represented by the specified expression using the specified list items and option label.
Public methodStatic memberDropDownListFor(Of TModel, TProperty)(HtmlHelper(Of TModel), Expression(Of Func(Of TModel, TProperty)), IEnumerable(Of SelectListItem), String, IDictionary(Of String, Object))Returns an HTML select element for each property in the object that is represented by the specified expression using the specified list items, option label, and HTML attributes.
Public methodStatic memberDropDownListFor(Of TModel, TProperty)(HtmlHelper(Of TModel), Expression(Of Func(Of TModel, TProperty)), IEnumerable(Of SelectListItem), String, Object)Returns an HTML select element for each property in the object that is represented by the specified expression using the specified list items, option label, and HTML attributes.
Public methodStatic memberListBox(HtmlHelper, String)Returns a multi-select select element using the specified HTML helper and the name of the form field.
Public methodStatic memberListBox(HtmlHelper, String, IEnumerable(Of SelectListItem))Returns a multi-select select element using the specified HTML helper, the name of the form field, and the specified list items.
Public methodStatic memberListBox(HtmlHelper, String, IEnumerable(Of SelectListItem), IDictionary(Of String, Object))Returns a multi-select select element using the specified HTML helper, the name of the form field, the specified list items, and the specified HMTL attributes.
Public methodStatic memberListBox(HtmlHelper, String, IEnumerable(Of SelectListItem), Object)Returns a multi-select select element using the specified HTML helper, the name of the form field, and the specified list items.
Public methodStatic memberListBoxFor(Of TModel, TProperty)(HtmlHelper(Of TModel), Expression(Of Func(Of TModel, TProperty)), IEnumerable(Of SelectListItem))Returns an HTML select element for each property in the object that is represented by the specified expression and using the specified list items.
Public methodStatic memberListBoxFor(Of TModel, TProperty)(HtmlHelper(Of TModel), Expression(Of Func(Of TModel, TProperty)), IEnumerable(Of SelectListItem), IDictionary(Of String, Object))Returns an HTML select element for each property in the object that is represented by the specified expression using the specified list items and HTML attributes.
Public methodStatic memberListBoxFor(Of TModel, TProperty)(HtmlHelper(Of TModel), Expression(Of Func(Of TModel, TProperty)), IEnumerable(Of SelectListItem), Object)Returns an HTML select element for each property in the object that is represented by the specified expression using the specified list items and HTML attributes.
Top

The SelectExtensions class contains methods that extend the HtmlHelper class. Each extension method renders an HTML select element. The DropDownList method renders an element that enables the user to select an item from a drop-down list. The ListBox method renders an element that enables the user to select from a scrolling list of items.

The following example shows how to use both the DropDownList and ListBox methods in a view. The ListBox control displays a list of book titles, from which the user can select one or more books. The DropDownList displays a list of pets, from which the user can select one pet. The selections are then displayed in another view.

The following classes define the data model that is used for a book and a pet.


Public Class Book
    Private _Id As Integer
    Public Property Id() As Integer
        Get
            Return _Id
        End Get
        Set(ByVal value As Integer)
            _Id = value
        End Set
    End Property

    Private _Title As String
    Public Property Title() As String
        Get
            Return _Title
        End Get
        Set(ByVal value As String)
            _Title = value
        End Set
    End Property
End Class

Public Class Pet
    Private _Id As Integer
    Public Property Id() As Integer
        Get
            Return _Id
        End Get
        Set(ByVal value As Integer)
            _Id = value
        End Set
    End Property

    Private _Name As String
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property
End Class


In the following example, the list of items for each control is created in the Index action method and passed to the view in the ViewDataDictionary object.


Shared bookList As List(Of Book) = New List(Of Book)
Shared petList As List(Of Pet) = New List(Of Pet)

Function Index() As ActionResult
    ViewData("Message") = "HTML Select Extensions for MVC"

    bookList.Add(New Book With {.Id = 1, _
        .Title = "Adventures of Huckleberry Finn"})
    bookList.Add(New Book With {.Id = 2, _
        .Title = "Crime and Punishment"})
    bookList.Add(New Book With {.Id = 3, _
        .Title = "David Copperfield"})
    bookList.Add(New Book With {.Id = 4, _
        .Title = "Gone with the Wind"})
    bookList.Add(New Book With {.Id = 5, _
        .Title = "Moby Dick"})
    bookList.Add(New Book With {.Id = 6, _
        .Title = "Origin of Species"})
    bookList.Add(New Book With {.Id = 7, _
        .Title = "War and Peace"})

    ViewData("Books") = From book In bookList _
                        Select New SelectListItem With _
                        {.Text = book.Title, .Value = book.Id.ToString()}

    petList.Add(New Pet With {.Id = 1, _
        .Name = "Dog"})
    petList.Add(New Pet With {.Id = 2, _
        .Name = "Cat"})
    petList.Add(New Pet With {.Id = 3, _
        .Name = "Hamster"})
    petList.Add(New Pet With {.Id = 4, _
        .Name = "Parrot"})
    petList.Add(New Pet With {.Id = 5, _
        .Name = "Gold fish"})
    petList.Add(New Pet With {.Id = 6, _
        .Name = "Mountain lion"})
    petList.Add(New Pet With {.Id = 7, _
        .Name = "Elephant"})

    ViewData("Pets") = From pet In petList _
                        Select New SelectListItem With _
                        {.Text = pet.Name, .Value = pet.Id.ToString()}

    Return View()
End Function


The following example shows a view that displays the entry form that contains the list box and the drop-down list. The list box is passed an anonymous object that defines the HMTL size attribute for the rendered list box.


<h2><%= Html.Encode(ViewData("Message")) %></h2>
<br />
<% Using (Html.BeginForm("Selection", "Home"))%>
    Select one or more books:<br />
    <%=Html.ListBox("BookIds", _
        CType(ViewData("Books"), IEnumerable(Of SelectListItem)), _
        New With {.size = "7"})%>
    <br /><br />
    Select a pet:<br /> 
    <%=Html.DropDownList("PetId", _
        CType(ViewData("Pets"), IEnumerable(Of SelectListItem)))%>
    <br /><br />
    <input type="submit" value="Submit" />
<% End Using%>


When the user submits the form, the Selection action method handles the request and renders the view that displays the selections.


Function Selection(ByVal bookIds As Integer(), ByVal petId As Integer) As ActionResult
    Dim bookMsg As String = ""
    Dim book As Book

    For Each book In bookList
        Dim i As Integer
        For i = 0 To bookIds.Count() - 1 Step 1
            If (book.Id = bookIds(i)) Then
                bookMsg = bookMsg + "Your book selection: <b>" _
                        + book.Title + "</b><br />"
            End If
        Next
    Next

    ViewData("books") = bookMsg

    Dim pet As Pet

    For Each pet In petList
        If (pet.Id = petId) Then
            ViewData("pet") = pet.Name
        End If
    Next

    Return View()
End Function


The following view displays the user selections.


<h2>HTML Select Extensions</h2>

<p><%=ViewData("books")%></p>
<p>Your pet selection: <b><%=ViewData("pet")%></b></p>


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show: