// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections; namespace Microsoft.AspNet.Mvc.Rendering { /// /// Represents a list that lets users select a single item. /// This class is typically rendered as an HTML <select> element with the specified collection /// of objects. /// public class SelectList : MultiSelectList { public SelectList(IEnumerable items) : this(items, selectedValue: null) { if (items == null) { throw new ArgumentNullException(nameof(items)); } } public SelectList(IEnumerable items, object selectedValue) : this(items, dataValueField: null, dataTextField: null, selectedValue: selectedValue) { if (items == null) { throw new ArgumentNullException(nameof(items)); } } public SelectList(IEnumerable items, string dataValueField, string dataTextField) : this(items, dataValueField, dataTextField, selectedValue: null) { if (items == null) { throw new ArgumentNullException(nameof(items)); } } public SelectList( IEnumerable items, string dataValueField, string dataTextField, object selectedValue) : base(items, dataValueField, dataTextField, ToEnumerable(selectedValue)) { if (items == null) { throw new ArgumentNullException(nameof(items)); } SelectedValue = selectedValue; } /// /// Initializes a new instance of the SelectList class by using the specified items for the list, /// the data value field, the data text field, a selected value, and the data group field. /// /// The items used to build each of the list. /// The data value field. Used to match the Value property of the corresponding /// . /// The data text field. Used to match the Text property of the corresponding /// . /// The selected values. Used to match the Selected property of the corresponding /// . /// The data group field. Used to match the Group property of the corresponding /// . public SelectList( IEnumerable items, string dataValueField, string dataTextField, object selectedValue, string dataGroupField) : base(items, dataValueField, dataTextField, ToEnumerable(selectedValue), dataGroupField) { if (items == null) { throw new ArgumentNullException(nameof(items)); } SelectedValue = selectedValue; } public object SelectedValue { get; private set; } private static IEnumerable ToEnumerable(object selectedValue) { return (selectedValue != null) ? new[] { selectedValue } : null; } } }