Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

SelectExtensions Class

.NET Framework 4

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)

public static 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<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<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 memberDropDownList(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 memberDropDownList(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 memberDropDownList(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 memberDropDownList(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 memberDropDownListFor<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 memberDropDownListFor<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 memberDropDownListFor<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 memberDropDownListFor<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 memberDropDownListFor<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 memberDropDownListFor<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 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<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<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 memberListBox(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 memberListBoxFor<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 memberListBoxFor<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 memberListBoxFor<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

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.


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", 
        (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.


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>


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:
© 2015 Microsoft