373 lines
18 KiB
C#
373 lines
18 KiB
C#
// 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.AspNet.Html;
|
|
|
|
namespace Microsoft.AspNet.Mvc.Rendering
|
|
{
|
|
/// <summary>
|
|
/// Select-related extensions for <see cref="IHtmlHelper"/> and <see cref="IHtmlHelper{TModel}"/>.
|
|
/// </summary>
|
|
public static class HtmlHelperSelectExtensions
|
|
{
|
|
/// <summary>
|
|
/// Returns a single-selection HTML <select> element for the <paramref name="expression"/>.
|
|
/// </summary>
|
|
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/> instance this method extends.</param>
|
|
/// <param name="expression">Expression name, relative to the current model.</param>
|
|
/// <returns>A new <see cref="IHtmlContent"/> containing the <select> element.</returns>
|
|
/// <remarks>
|
|
/// Combines <see cref="ViewFeatures.TemplateInfo.HtmlFieldPrefix"/> and <paramref name="expression"/> to set
|
|
/// <select> element's "name" attribute. Sanitizes <paramref name="expression"/> to set element's "id"
|
|
/// attribute.
|
|
/// </remarks>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a single-selection HTML <select> element for the <paramref name="expression"/>, using the
|
|
/// option label.
|
|
/// </summary>
|
|
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/> instance this method extends.</param>
|
|
/// <param name="expression">Expression name, relative to the current model.</param>
|
|
/// <param name="optionLabel">
|
|
/// The text for a default empty item. Does not include such an item if argument is <c>null</c>.
|
|
/// </param>
|
|
/// <returns>A new <see cref="IHtmlContent"/> containing the <select> element.</returns>
|
|
/// <remarks>
|
|
/// Combines <see cref="ViewFeatures.TemplateInfo.HtmlFieldPrefix"/> and <paramref name="expression"/> to set
|
|
/// <select> element's "name" attribute. Sanitizes <paramref name="expression"/> to set element's "id"
|
|
/// attribute.
|
|
/// </remarks>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a single-selection HTML <select> element for the <paramref name="expression"/>, using the
|
|
/// specified list items.
|
|
/// </summary>
|
|
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/> instance this method extends.</param>
|
|
/// <param name="expression">Expression name, relative to the current model.</param>
|
|
/// <param name="selectList">
|
|
/// A collection of <see cref="SelectListItem"/> objects used to populate the <select> element with
|
|
/// <optgroup> and <option> elements.
|
|
/// </param>
|
|
/// <returns>A new <see cref="IHtmlContent"/> containing the <select> element.</returns>
|
|
/// <remarks>
|
|
/// Combines <see cref="ViewFeatures.TemplateInfo.HtmlFieldPrefix"/> and <paramref name="expression"/> to set
|
|
/// <select> element's "name" attribute. Sanitizes <paramref name="expression"/> to set element's "id"
|
|
/// attribute.
|
|
/// </remarks>
|
|
public static IHtmlContent DropDownList(
|
|
this IHtmlHelper htmlHelper,
|
|
string expression,
|
|
IEnumerable<SelectListItem> selectList)
|
|
{
|
|
if (htmlHelper == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(htmlHelper));
|
|
}
|
|
|
|
return htmlHelper.DropDownList(expression, selectList, optionLabel: null, htmlAttributes: null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a single-selection HTML <select> element for the <paramref name="expression"/>, using the
|
|
/// specified list items and HTML attributes.
|
|
/// </summary>
|
|
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/> instance this method extends.</param>
|
|
/// <param name="expression">Expression name, relative to the current model.</param>
|
|
/// <param name="selectList">
|
|
/// A collection of <see cref="SelectListItem"/> objects used to populate the <select> element with
|
|
/// <optgroup> and <option> elements.
|
|
/// </param>
|
|
/// <param name="htmlAttributes">
|
|
/// An <see cref="object"/> that contains the HTML attributes for the <select> element. Alternatively, an
|
|
/// <see cref="IDictionary{string, object}"/> instance containing the HTML attributes.
|
|
/// </param>
|
|
/// <returns>A new <see cref="IHtmlContent"/> containing the <select> element.</returns>
|
|
/// <remarks>
|
|
/// Combines <see cref="ViewFeatures.TemplateInfo.HtmlFieldPrefix"/> and <paramref name="expression"/> to set
|
|
/// <select> element's "name" attribute. Sanitizes <paramref name="expression"/> to set element's "id"
|
|
/// attribute.
|
|
/// </remarks>
|
|
public static IHtmlContent DropDownList(
|
|
this IHtmlHelper htmlHelper,
|
|
string expression,
|
|
IEnumerable<SelectListItem> selectList,
|
|
object htmlAttributes)
|
|
{
|
|
if (htmlHelper == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(htmlHelper));
|
|
}
|
|
|
|
return htmlHelper.DropDownList(expression, selectList, optionLabel: null, htmlAttributes: htmlAttributes);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a single-selection HTML <select> element for the <paramref name="expression"/>, using the
|
|
/// specified list items and option label.
|
|
/// </summary>
|
|
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/> instance this method extends.</param>
|
|
/// <param name="expression">Expression name, relative to the current model.</param>
|
|
/// <param name="selectList">
|
|
/// A collection of <see cref="SelectListItem"/> objects used to populate the <select> element with
|
|
/// <optgroup> and <option> elements.
|
|
/// </param>
|
|
/// <param name="optionLabel">
|
|
/// The text for a default empty item. Does not include such an item if argument is <c>null</c>.
|
|
/// </param>
|
|
/// <returns>A new <see cref="IHtmlContent"/> containing the <select> element.</returns>
|
|
/// <remarks>
|
|
/// Combines <see cref="ViewFeatures.TemplateInfo.HtmlFieldPrefix"/> and <paramref name="expression"/> to set
|
|
/// <select> element's "name" attribute. Sanitizes <paramref name="expression"/> to set element's "id"
|
|
/// attribute.
|
|
/// </remarks>
|
|
public static IHtmlContent DropDownList(
|
|
this IHtmlHelper htmlHelper,
|
|
string expression,
|
|
IEnumerable<SelectListItem> selectList,
|
|
string optionLabel)
|
|
{
|
|
if (htmlHelper == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(htmlHelper));
|
|
}
|
|
|
|
return htmlHelper.DropDownList(expression, selectList, optionLabel, htmlAttributes: null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a single-selection HTML <select> element for the <paramref name="expression"/>, using the
|
|
/// specified list items.
|
|
/// </summary>
|
|
/// <param name="htmlHelper">The <see cref="IHtmlHelper{TModel}"/> instance this method extends.</param>
|
|
/// <param name="expression">An expression to be evaluated against the current model.</param>
|
|
/// <param name="selectList">
|
|
/// A collection of <see cref="SelectListItem"/> objects used to populate the <select> element with
|
|
/// <optgroup> and <option> elements.
|
|
/// </param>
|
|
/// <typeparam name="TModel">The type of the model.</typeparam>
|
|
/// <typeparam name="TResult">The type of the <paramref name="expression"/> result.</typeparam>
|
|
/// <returns>A new <see cref="IHtmlContent"/> containing the <select> element.</returns>
|
|
/// <remarks>
|
|
/// Combines <see cref="ViewFeatures.TemplateInfo.HtmlFieldPrefix"/> and the string representation of the
|
|
/// <paramref name="expression"/> to set <select> element's "name" attribute. Sanitizes the string
|
|
/// representation of the <paramref name="expression"/> to set element's "id" attribute.
|
|
/// </remarks>
|
|
public static IHtmlContent DropDownListFor<TModel, TResult>(
|
|
this IHtmlHelper<TModel> htmlHelper,
|
|
Expression<Func<TModel, TResult>> expression,
|
|
IEnumerable<SelectListItem> 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a single-selection HTML <select> element for the <paramref name="expression"/>, using the
|
|
/// specified list items and HTML attributes.
|
|
/// </summary>
|
|
/// <param name="htmlHelper">The <see cref="IHtmlHelper{TModel}"/> instance this method extends.</param>
|
|
/// <param name="expression">An expression to be evaluated against the current model.</param>
|
|
/// <param name="selectList">
|
|
/// A collection of <see cref="SelectListItem"/> objects used to populate the <select> element with
|
|
/// <optgroup> and <option> elements.
|
|
/// </param>
|
|
/// <param name="htmlAttributes">
|
|
/// An <see cref="object"/> that contains the HTML attributes for the <select> element. Alternatively, an
|
|
/// <see cref="IDictionary{string, object}"/> instance containing the HTML attributes.
|
|
/// </param>
|
|
/// <typeparam name="TModel">The type of the model.</typeparam>
|
|
/// <typeparam name="TResult">The type of the <paramref name="expression"/> result.</typeparam>
|
|
/// <returns>A new <see cref="IHtmlContent"/> containing the <select> element.</returns>
|
|
/// <remarks>
|
|
/// Combines <see cref="ViewFeatures.TemplateInfo.HtmlFieldPrefix"/> and the string representation of the
|
|
/// <paramref name="expression"/> to set <select> element's "name" attribute. Sanitizes the string
|
|
/// representation of the <paramref name="expression"/> to set element's "id" attribute.
|
|
/// </remarks>
|
|
public static IHtmlContent DropDownListFor<TModel, TResult>(
|
|
this IHtmlHelper<TModel> htmlHelper,
|
|
Expression<Func<TModel, TResult>> expression,
|
|
IEnumerable<SelectListItem> 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a single-selection HTML <select> element for the <paramref name="expression"/>, using the
|
|
/// specified list items and option label.
|
|
/// </summary>
|
|
/// <param name="htmlHelper">The <see cref="IHtmlHelper{TModel}"/> instance this method extends.</param>
|
|
/// <param name="expression">An expression to be evaluated against the current model.</param>
|
|
/// <param name="selectList">
|
|
/// A collection of <see cref="SelectListItem"/> objects used to populate the <select> element with
|
|
/// <optgroup> and <option> elements.
|
|
/// </param>
|
|
/// <param name="optionLabel">
|
|
/// The text for a default empty item. Does not include such an item if argument is <c>null</c>.
|
|
/// </param>
|
|
/// <typeparam name="TModel">The type of the model.</typeparam>
|
|
/// <typeparam name="TResult">The type of the <paramref name="expression"/> result.</typeparam>
|
|
/// <returns>A new <see cref="IHtmlContent"/> containing the <select> element.</returns>
|
|
/// <remarks>
|
|
/// Combines <see cref="ViewFeatures.TemplateInfo.HtmlFieldPrefix"/> and the string representation of the
|
|
/// <paramref name="expression"/> to set <select> element's "name" attribute. Sanitizes the string
|
|
/// representation of the <paramref name="expression"/> to set element's "id" attribute.
|
|
/// </remarks>
|
|
public static IHtmlContent DropDownListFor<TModel, TResult>(
|
|
this IHtmlHelper<TModel> htmlHelper,
|
|
Expression<Func<TModel, TResult>> expression,
|
|
IEnumerable<SelectListItem> 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a multi-selection <select> element for the <paramref name="expression"/>.
|
|
/// </summary>
|
|
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/> instance this method extends.</param>
|
|
/// <param name="expression">Expression name, relative to the current model.</param>
|
|
/// <returns>A new <see cref="IHtmlContent"/> containing the <select> element.</returns>
|
|
/// <remarks>
|
|
/// Combines <see cref="ViewFeatures.TemplateInfo.HtmlFieldPrefix"/> and <paramref name="expression"/> to set
|
|
/// <select> element's "name" attribute. Sanitizes <paramref name="expression"/> to set element's "id"
|
|
/// attribute.
|
|
/// </remarks>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a multi-selection <select> element for the <paramref name="expression"/>, using the
|
|
/// specified list items.
|
|
/// </summary>
|
|
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/> instance this method extends.</param>
|
|
/// <param name="expression">Expression name, relative to the current model.</param>
|
|
/// <param name="selectList">
|
|
/// A collection of <see cref="SelectListItem"/> objects used to populate the <select> element with
|
|
/// <optgroup> and <option> elements.
|
|
/// </param>
|
|
/// <returns>A new <see cref="IHtmlContent"/> containing the <select> element.</returns>
|
|
/// <remarks>
|
|
/// Combines <see cref="ViewFeatures.TemplateInfo.HtmlFieldPrefix"/> and <paramref name="expression"/> to set
|
|
/// <select> element's "name" attribute. Sanitizes <paramref name="expression"/> to set element's "id"
|
|
/// attribute.
|
|
/// </remarks>
|
|
public static IHtmlContent ListBox(
|
|
this IHtmlHelper htmlHelper,
|
|
string expression,
|
|
IEnumerable<SelectListItem> selectList)
|
|
{
|
|
if (htmlHelper == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(htmlHelper));
|
|
}
|
|
|
|
return htmlHelper.ListBox(expression, selectList, htmlAttributes: null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a multi-selection <select> element for the <paramref name="expression"/>, using the
|
|
/// specified list items.
|
|
/// </summary>
|
|
/// <param name="htmlHelper">The <see cref="IHtmlHelper{TModel}"/> instance this method extends.</param>
|
|
/// <param name="expression">An expression to be evaluated against the current model.</param>
|
|
/// <param name="selectList">
|
|
/// A collection of <see cref="SelectListItem"/> objects used to populate the <select> element with
|
|
/// <optgroup> and <option> elements.
|
|
/// </param>
|
|
/// <typeparam name="TModel">The type of the model.</typeparam>
|
|
/// <typeparam name="TResult">The type of the <paramref name="expression"/> result.</typeparam>
|
|
/// <returns>A new <see cref="IHtmlContent"/> containing the <select> element.</returns>
|
|
/// <remarks>
|
|
/// Combines <see cref="ViewFeatures.TemplateInfo.HtmlFieldPrefix"/> and the string representation of the
|
|
/// <paramref name="expression"/> to set <select> element's "name" attribute. Sanitizes the string
|
|
/// representation of the <paramref name="expression"/> to set element's "id" attribute.
|
|
/// </remarks>
|
|
public static IHtmlContent ListBoxFor<TModel, TResult>(
|
|
this IHtmlHelper<TModel> htmlHelper,
|
|
Expression<Func<TModel, TResult>> expression,
|
|
IEnumerable<SelectListItem> selectList)
|
|
{
|
|
if (htmlHelper == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(htmlHelper));
|
|
}
|
|
|
|
if (expression == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(expression));
|
|
}
|
|
|
|
return htmlHelper.ListBoxFor(expression, selectList, htmlAttributes: null);
|
|
}
|
|
}
|
|
}
|