// 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.Generic;
using System.Linq.Expressions;
using Microsoft.AspNetCore.Html;
namespace Microsoft.AspNetCore.Mvc.Rendering
{
///
/// Select-related extensions for and .
///
public static class HtmlHelperSelectExtensions
{
///
/// Returns a single-selection HTML <select> element for the . Adds
/// <option> elements based on the entry with full name. Adds a
/// "selected" attribute to an <option> if its (if non-null) or
/// matches the first non-null value found in:
/// the entry with full name, or
/// the evaluated against .
/// See for more information about a "full name".
///
/// The instance this method extends.
/// Expression name, relative to the current model.
/// A new containing the <select> element.
///
///
/// Combines and to set
/// <select> element's "name" attribute. Sanitizes to set element's "id"
/// attribute.
///
///
/// The entry with full name must be a non-null collection of
/// objects.
///
///
public static IHtmlContent DropDownList(this IHtmlHelper htmlHelper, string expression)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.DropDownList(expression, selectList: null, optionLabel: null, htmlAttributes: null);
}
///
/// Returns a single-selection HTML <select> element for the . Adds
/// <option> elements based on and the
/// entry with full name. Adds a "selected" attribute to an <option> if its
/// (if non-null) or matches the
/// first non-null value found in:
/// the entry with full name, or
/// the evaluated against .
/// See for more information about a "full name".
///
/// The instance this method extends.
/// Expression name, relative to the current model.
///
/// The text for a default empty item. Does not include such an item if argument is null.
///
/// A new containing the <select> element.
///
///
/// Combines and to set
/// <select> element's "name" attribute. Sanitizes to set element's "id"
/// attribute.
///
///
/// The entry with full name must be a non-null collection of
/// objects.
///
///
public static IHtmlContent DropDownList(
this IHtmlHelper htmlHelper,
string expression,
string optionLabel)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.DropDownList(
expression,
selectList: null,
optionLabel: optionLabel,
htmlAttributes: null);
}
///
/// Returns a single-selection HTML <select> element for the . Adds
/// <option> elements based on . Adds a "selected" attribute to an
/// <option> if its (if non-null) or
/// matches the first non-null value found in:
/// the entry with full name,
/// the entry with full name (unless used instead of
/// ), or
/// the evaluated against .
/// See for more information about a "full name".
///
/// The instance this method extends.
/// Expression name, relative to the current model.
///
/// A collection of objects used to populate the <select> element with
/// <optgroup> and <option> elements. If null, uses the
/// entry with full name and that entry must be a collection of objects.
///
/// A new containing the <select> element.
///
/// Combines and to set
/// <select> element's "name" attribute. Sanitizes to set element's "id"
/// attribute.
///
public static IHtmlContent DropDownList(
this IHtmlHelper htmlHelper,
string expression,
IEnumerable selectList)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.DropDownList(expression, selectList, optionLabel: null, htmlAttributes: null);
}
///
/// Returns a single-selection HTML <select> element for the . Adds
/// <option> elements based on . Adds a "selected" attribute to an
/// <option> if its (if non-null) or
/// matches the first non-null value found in:
/// the entry with full name,
/// the entry with full name (unless used instead of
/// ), or
/// the evaluated against .
/// See for more information about a "full name".
///
/// The instance this method extends.
/// Expression name, relative to the current model.
///
/// A collection of objects used to populate the <select> element with
/// <optgroup> and <option> elements. If null, uses the
/// entry with full name and that entry must be a collection of objects.
///
///
/// An that contains the HTML attributes for the <select> element. Alternatively, an
/// instance containing the HTML attributes.
///
/// A new containing the <select> element.
///
/// Combines and to set
/// <select> element's "name" attribute. Sanitizes to set element's "id"
/// attribute.
///
public static IHtmlContent DropDownList(
this IHtmlHelper htmlHelper,
string expression,
IEnumerable selectList,
object htmlAttributes)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.DropDownList(expression, selectList, optionLabel: null, htmlAttributes: htmlAttributes);
}
///
/// Returns a single-selection HTML <select> element for the . Adds
/// <option> elements based on and . Adds a
/// "selected" attribute to an <option> if its (if non-null) or
/// matches the first non-null value found in:
/// the entry with full name,
/// the entry with full name (unless used instead of
/// ), or
/// the evaluated against .
/// See for more information about a "full name".
///
/// The instance this method extends.
/// Expression name, relative to the current model.
///
/// A collection of objects used to populate the <select> element with
/// <optgroup> and <option> elements. If null, uses the
/// entry with full name and that entry must be a collection of objects.
///
///
/// The text for a default empty item. Does not include such an item if argument is null.
///
/// A new containing the <select> element.
///
/// Combines and to set
/// <select> element's "name" attribute. Sanitizes to set element's "id"
/// attribute.
///
public static IHtmlContent DropDownList(
this IHtmlHelper htmlHelper,
string expression,
IEnumerable selectList,
string optionLabel)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.DropDownList(expression, selectList, optionLabel, htmlAttributes: null);
}
///
/// Returns a single-selection HTML <select> element for the . Adds
/// <option> elements based on . Adds a "selected" attribute to an
/// <option> if its (if non-null) or
/// matches the first non-null value found in:
/// the entry with full name, or
/// the evaluated against .
/// See for more information about a "full name".
///
/// The instance this method extends.
/// An expression to be evaluated against the current model.
///
/// A collection of objects used to populate the <select> element with
/// <optgroup> and <option> elements. If null, uses the
/// entry with full name and that entry must be a collection of objects.
///
/// The type of the model.
/// The type of the result.
/// A new containing the <select> element.
///
/// Combines and the string representation of the
/// to set <select> element's "name" attribute. Sanitizes the string
/// representation of the to set element's "id" attribute.
///
public static IHtmlContent DropDownListFor(
this IHtmlHelper htmlHelper,
Expression> expression,
IEnumerable selectList)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
if (expression == null)
{
throw new ArgumentNullException(nameof(expression));
}
return htmlHelper.DropDownListFor(expression, selectList, optionLabel: null, htmlAttributes: null);
}
///
/// Returns a single-selection HTML <select> element for the . Adds
/// <option> elements based on . Adds a "selected" attribute to an
/// <option> if its (if non-null) or
/// matches the first non-null value found in:
/// the entry with full name, or
/// the evaluated against .
/// See for more information about a "full name".
///
/// The instance this method extends.
/// An expression to be evaluated against the current model.
///
/// A collection of objects used to populate the <select> element with
/// <optgroup> and <option> elements. If null, uses the
/// entry with full name and that entry must be a collection of objects.
///
///
/// An that contains the HTML attributes for the <select> element. Alternatively, an
/// instance containing the HTML attributes.
///
/// The type of the model.
/// The type of the result.
/// A new containing the <select> element.
///
/// Combines and the string representation of the
/// to set <select> element's "name" attribute. Sanitizes the string
/// representation of the to set element's "id" attribute.
///
public static IHtmlContent DropDownListFor(
this IHtmlHelper htmlHelper,
Expression> expression,
IEnumerable selectList,
object htmlAttributes)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
if (expression == null)
{
throw new ArgumentNullException(nameof(expression));
}
return htmlHelper.DropDownListFor(
expression,
selectList,
optionLabel: null,
htmlAttributes: htmlAttributes);
}
///
/// Returns a single-selection HTML <select> element for the . Adds
/// <option> elements based on and . Adds a
/// "selected" attribute to an <option> if its (if non-null) or
/// matches the first non-null value found in:
/// the entry with full name, or
/// the evaluated against .
/// See for more information about a "full name".
///
/// The instance this method extends.
/// An expression to be evaluated against the current model.
///
/// A collection of objects used to populate the <select> element with
/// <optgroup> and <option> elements. If null, uses the
/// entry with full name and that entry must be a collection of objects.
///
///
/// The text for a default empty item. Does not include such an item if argument is null.
///
/// The type of the model.
/// The type of the result.
/// A new containing the <select> element.
///
/// Combines and the string representation of the
/// to set <select> element's "name" attribute. Sanitizes the string
/// representation of the to set element's "id" attribute.
///
public static IHtmlContent DropDownListFor(
this IHtmlHelper htmlHelper,
Expression> expression,
IEnumerable selectList,
string optionLabel)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
if (expression == null)
{
throw new ArgumentNullException(nameof(expression));
}
return htmlHelper.DropDownListFor(expression, selectList, optionLabel, htmlAttributes: null);
}
///
/// Returns a multi-selection <select> element for the . Adds
/// <option> elements based on the entry with full name. Adds a
/// "selected" attribute to an <option> if its (if non-null) or
/// matches the first non-null value found in:
/// the entry with full name, or
/// the evaluated against .
/// See for more information about a "full name".
///
/// The instance this method extends.
/// Expression name, relative to the current model.
/// A new containing the <select> element.
///
///
/// Combines and to set
/// <select> element's "name" attribute. Sanitizes to set element's "id"
/// attribute.
///
///
/// The entry with full name must be a non-null collection of
/// objects.
///
///
public static IHtmlContent ListBox(this IHtmlHelper htmlHelper, string expression)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.ListBox(expression, selectList: null, htmlAttributes: null);
}
///
/// Returns a multi-selection <select> element for the . Adds
/// <option> elements based on . Adds a "selected" attribute to an
/// <option> if its (if non-null) or
/// matches the first non-null value found in:
/// the entry with full name,
/// the entry with full name (unless used instead of
/// ), or
/// the evaluated against .
/// See for more information about a "full name".
///
/// The instance this method extends.
/// Expression name, relative to the current model.
///
/// A collection of objects used to populate the <select> element with
/// <optgroup> and <option> elements. If null, uses the
/// entry with full name and that entry must be a collection of objects.
///
/// A new containing the <select> element.
///
/// Combines and to set
/// <select> element's "name" attribute. Sanitizes to set element's "id"
/// attribute.
///
public static IHtmlContent ListBox(
this IHtmlHelper htmlHelper,
string expression,
IEnumerable selectList)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.ListBox(expression, selectList, htmlAttributes: null);
}
///
/// Returns a multi-selection <select> element for the . Adds
/// <option> elements based on . Adds a "selected" attribute to an
/// <option> if its (if non-null) or
/// matches the first non-null value found in:
/// the entry with full name, or
/// the evaluated against .
/// See for more information about a "full name".
///
/// The instance this method extends.
/// An expression to be evaluated against the current model.
///
/// A collection of objects used to populate the <select> element with
/// <optgroup> and <option> elements. If null, uses the
/// entry with full name and that entry must be a collection of objects.
///
/// The type of the model.
/// The type of the result.
/// A new containing the <select> element.
///
/// Combines and the string representation of the
/// to set <select> element's "name" attribute. Sanitizes the string
/// representation of the to set element's "id" attribute.
///
public static IHtmlContent ListBoxFor(
this IHtmlHelper htmlHelper,
Expression> expression,
IEnumerable selectList)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
if (expression == null)
{
throw new ArgumentNullException(nameof(expression));
}
return htmlHelper.ListBoxFor(expression, selectList, htmlAttributes: null);
}
}
}