Delen via


SelectExtensions Class

Represents support for making selections in a list.

Inheritance Hierarchy

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

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

Syntax

'Declaration
<ExtensionAttribute> _
Public NotInheritable Class SelectExtensions
public static class SelectExtensions
[ExtensionAttribute]
public ref class SelectExtensions abstract sealed

Methods

  Name Description
Public methodStatic member DropDownList(HtmlHelper, String) Returns a single-selection select element using the specified HTML helper and the name of the form field.
Public methodStatic member DropDownList(HtmlHelper, String, IEnumerable<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 member DropDownList(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 member DropDownList(HtmlHelper, String, IEnumerable<SelectListItem>, IDictionary<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 member DropDownList(HtmlHelper, String, IEnumerable<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 member DropDownList(HtmlHelper, String, IEnumerable<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 member DropDownList(HtmlHelper, String, IEnumerable<SelectListItem>, String, IDictionary<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 member DropDownList(HtmlHelper, String, IEnumerable<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 member DropDownListFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, IEnumerable<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 member DropDownListFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, IEnumerable<SelectListItem>, IDictionary<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 member DropDownListFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, IEnumerable<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 member DropDownListFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, IEnumerable<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 member DropDownListFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, IEnumerable<SelectListItem>, String, IDictionary<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 member DropDownListFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, IEnumerable<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 member ListBox(HtmlHelper, String) Returns a multi-select select element using the specified HTML helper and the name of the form field.
Public methodStatic member ListBox(HtmlHelper, String, IEnumerable<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 member ListBox(HtmlHelper, String, IEnumerable<SelectListItem>, IDictionary<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 member ListBox(HtmlHelper, String, IEnumerable<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 member ListBoxFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, IEnumerable<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 member ListBoxFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, IEnumerable<SelectListItem>, IDictionary<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 member ListBoxFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, IEnumerable<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

Remarks

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.

Examples

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
static List<Book> bookList = new List<Book>();
static List<Pet> petList = new List<Pet>();

public ActionResult Index()
{
    ViewData["Message"] = "HTML Select Extensions for MVC";

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

    ViewData["Books"] = from book in bookList
                        select new SelectListItem 
                        { 
                            Text = book.Title, 
                            Value = book.Id.ToString() 
                        };

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

    ViewData["Pets"] = from pet in petList
                        select new SelectListItem
                        {
                            Text = pet.Name,
                            Value = pet.Id.ToString()
                        };
    return View();
}

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%>
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
<br />
<% using(Html.BeginForm("Selection", "Home")) %>
<% { %>
    Select one or more books:<br />
    <%= Html.ListBox("BookIds", 
        (IEnumerable<SelectListItem>)ViewData["Books"], 
        new {size="7"}) %>
    <br /><br />
    Select a pet:<br /> 
    <%= Html.DropDownList("PetId", 
        (IEnumerable<SelectListItem>)ViewData["Pets"]) %>
    <br /><br />
    <input type="submit" value="Submit" />
<% } %>

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
public ActionResult Selection(int[] bookIds, int petId)
{
    string bookMsg = "";

    foreach (Book book in bookList)
    {
        for (int i = 0; i < bookIds.Count(); i++)
        {
            if (book.Id == bookIds[i])
            {
                bookMsg = bookMsg + "Your book selection: <b>" 
                    + book.Title + "</b><br />";
            }
        }
    }

    ViewData["books"] = bookMsg;

    foreach (Pet pet in petList)
    {
        if (pet.Id == petId)
        {
            ViewData["pet"] = pet.Name;
        }
    }

    return View();
}

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>
<h2>HTML Select Extensions</h2>

<p><%= ViewData["books"] %></p>
<p>Your pet selection: <b><%= ViewData["pet"] %></b></p>

Thread Safety

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

See Also

Reference

System.Web.Mvc.Html Namespace

Other Resources

Rendering a Form in ASP.NET MVC Using HTML Helpers