Html.DropDownList()的用法
原始碼
using System.Collections;
using System.Diagnostics.CodeAnalysis;
namespace System.Web.Mvc
{
[SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Justification = "This is a shipped API")]
public class SelectList : MultiSelectList
{
public SelectList(IEnumerable items)
: this(items, selectedValue: null)
{
}
public SelectList(IEnumerable items, object selectedValue)
: this(items, dataValueField: null, dataTextField: null, selectedValue: selectedValue)
{
}
/// <summary>
/// Initializes a new instance of the SelectList class by using the specified items for the list,
/// the selected value, and the disabled values.
/// </summary>
/// <param name="items">The items used to build each <see cref="SelectListItem"/> of the list.</param>
/// <param name="selectedValue">The selected value. Used to match the Selected property of the corresponding
/// <see cref="SelectListItem"/>.</param>
/// <param name="disabledValues">The disabled values. Used to match the Disabled property of the corresponding
/// <see cref="SelectListItem"/>.</param>
public SelectList(IEnumerable items, object selectedValue, IEnumerable disabledValues)
: this(items,
dataValueField: null,
dataTextField: null,
selectedValue: selectedValue,
disabledValues: disabledValues)
{
}
public SelectList(IEnumerable items, string dataValueField, string dataTextField)
: this(items, dataValueField, dataTextField, selectedValue: null)
{
}
public SelectList(IEnumerable items, string dataValueField, string dataTextField, object selectedValue)
: base(items, dataValueField, dataTextField, ToEnumerable(selectedValue))
{
SelectedValue = selectedValue;
}
/// <summary>
/// Initializes a new instance of the SelectList class by using the specified items for the list,
/// the data value field, the data text field, the data group field, and the selected value.
/// </summary>
/// <param name="items">The items used to build each <see cref="SelectListItem"/> of the list.</param>
/// <param name="dataValueField">The data value field. Used to match the Value property of the corresponding
/// <see cref="SelectListItem"/>.</param>
/// <param name="dataTextField">The data text field. Used to match the Text property of the corresponding
/// <see cref="SelectListItem"/>.</param>
/// <param name="dataGroupField">The data group field. Used to match the Group property of the corresponding
/// <see cref="SelectListItem"/>.</param>
/// <param name="selectedValue">The selected value. Used to match the Selected property of the corresponding
/// <see cref="SelectListItem"/>.</param>
public SelectList(IEnumerable items,
string dataValueField,
string dataTextField,
string dataGroupField,
object selectedValue)
: base(items, dataValueField, dataTextField, dataGroupField, ToEnumerable(selectedValue))
{
SelectedValue = selectedValue;
}
/// <summary>
/// Initializes a new instance of the SelectList class by using the specified items for the list,
/// the data value field, the data text field, the selected value, and the disabled values.
/// </summary>
/// <param name="items">The items used to build each <see cref="SelectListItem"/> of the list.</param>
/// <param name="dataValueField">The data value field. Used to match the Value property of the corresponding
/// <see cref="SelectListItem"/>.</param>
/// <param name="dataTextField">The data text field. Used to match the Text property of the corresponding
/// <see cref="SelectListItem"/>.</param>
/// <param name="selectedValue">The selected value. Used to match the Selected property of the corresponding
/// <see cref="SelectListItem"/>.</param>
/// <param name="disabledValues">The disabled values. Used to match the Disabled property of the corresponding
/// <see cref="SelectListItem"/>.</param>
public SelectList(IEnumerable items,
string dataValueField,
string dataTextField,
object selectedValue,
IEnumerable disabledValues)
: base(items, dataValueField, dataTextField, ToEnumerable(selectedValue), disabledValues)
{
SelectedValue = selectedValue;
}
/// <summary>
/// Initializes a new instance of the SelectList class by using the specified items for the list,
/// the data value field, the data text field, the data group field, the selected value, and the disabled
/// values.
/// </summary>
/// <param name="items">The items used to build each <see cref="SelectListItem"/> of the list.</param>
/// <param name="dataValueField">The data value field. Used to match the Value property of the corresponding
/// <see cref="SelectListItem"/>.</param>
/// <param name="dataTextField">The data text field. Used to match the Text property of the corresponding
/// <see cref="SelectListItem"/>.</param>
/// <param name="dataGroupField">The data group field. Used to match the Group property of the corresponding
/// <see cref="SelectListItem"/>.</param>
/// <param name="selectedValue">The selected value. Used to match the Selected property of the corresponding
/// <see cref="SelectListItem"/>.</param>
/// <param name="disabledValues">The disabled values. Used to match the Disabled property of the corresponding
/// <see cref="SelectListItem"/>.</param>
public SelectList(IEnumerable items,
string dataValueField,
string dataTextField,
string dataGroupField,
object selectedValue,
IEnumerable disabledValues)
: base(items, dataValueField, dataTextField, dataGroupField, ToEnumerable(selectedValue), disabledValues)
{
SelectedValue = selectedValue;
}
/// <summary>
/// Initializes a new instance of the SelectList class by using the specified items for the list,
/// the data value field, the data text field, the data group field. the selected value, the disabled values,
/// and the disabled groups.
/// </summary>
/// <param name="items">The items used to build each <see cref="SelectListItem"/> of the list.</param>
/// <param name="dataValueField">The data value field. Used to match the Value property of the corresponding
/// <see cref="SelectListItem"/>.</param>
/// <param name="dataTextField">The data text field. Used to match the Text property of the corresponding
/// <see cref="SelectListItem"/>.</param>
/// <param name="dataGroupField">The data group field. Used to match the Group property of the corresponding
/// <see cref="SelectListItem"/>.</param>
/// <param name="selectedValue">The selected value. Used to match the Selected property of the corresponding
/// <see cref="SelectListItem"/>.</param>
/// <param name="disabledValues">The disabled values. Used to match the Disabled property of the corresponding
/// <see cref="SelectListItem"/>.</param>
/// <param name="disabledGroups">The disabled groups. Used to match the Disabled property of the corresponding
/// <see cref="SelectListGroup"/>.</param>
public SelectList(IEnumerable items,
string dataValueField,
string dataTextField,
string dataGroupField,
object selectedValue,
IEnumerable disabledValues,
IEnumerable disabledGroups)
: base(items,
dataValueField,
dataTextField,
dataGroupField,
ToEnumerable(selectedValue),
disabledValues,
disabledGroups)
{
SelectedValue = selectedValue;
}
public object SelectedValue { get; private set; }
private static IEnumerable ToEnumerable(object selectedValue)
{
return (selectedValue != null) ? new object[] { selectedValue } : null;
}
}
}
SelectListItem.cs
namespace System.Web.Mvc
{
public class SelectListItem
{
/// <summary>
/// Gets or sets a value that indicates whether this <see cref="SelectListItem"/> is disabled.
/// </summary>
public bool Disabled { get; set; }
/// <summary>
/// Represents the optgroup HTML element this item is wrapped into.
/// In a select list, multiple groups with the same name are supported.
/// They are compared with reference equality.
/// </summary>
public SelectListGroup Group { get; set; }
public bool Selected { get; set; }
public string Text { get; set; }
public string Value { get; set; }
}
}
Html.DropDownList()
賦預設值:
頁面程式碼如下:
<%
List<SelectListItem> list = new List<SelectListItem> {
new SelectListItem { Text = "啟用", Value = "0",Selected = true},
new SelectListItem { Text = "禁用", Value = "1" } };
%>//list儲存dropdownlist的預設值
<%=Html.DropDownList("state",list,Model.state) %> //state為實體的屬性,預設選中"啟用"
Html.DropDownList()
從資料庫讀取值:
頁面程式碼如下:
<%= Html.DropDownList("Category", ViewData["Categories"] as SelectList,"--請選擇--",new { @class = "my-select-css-class" } )
%>
Controllers
程式碼:
public ActionResult Create()
{
List<Category> categories = categoryService.GetAll();
ViewData["Categories"] = new SelectList(categories, "Id", "Name");
return View();
}
下面是我自己的程式碼:
頁面程式碼如下:
<!--資料庫提取資料 -->
<p>產品分類:<%=Html.DropDownList("category")%></p>
<!--手動設定資料-->
<p>狀態:<%=Html.DropDownList("status")%></p>
Controllers
程式碼:
ViewData["category"] = new SelectList(CategoryManager.GetList(),"id","name");
List<SelectListItem> list = new List<SelectListItem> {
new SelectListItem { Text = "啟用", Value = "0",Selected = true},
new SelectListItem { Text = "禁用", Value = "1" }
};
ViewData["status"] = list;
相關文章
- Qdrant用法;Qdrant在langchain裡的用法LangChain
- indexOf()的用法Index
- Pythonyield的用法Python
- Promise的用法Promise
- errno的用法
- SQL AS 的用法SQL
- confirm的用法
- SUBMIT 的用法MIT
- tie的用法
- volatile的用法
- contentProvider的用法IDE
- using的用法
- openssl的用法
- RDD用法與例項(五):glom的用法
- SqlServer的with(nolock)的用法SQLServer
- oracle comment on的用法Oracle
- React Hooks 的用法ReactHook
- Node assert 的用法
- MongoDB的基本用法MongoDB
- Vue slot的用法Vue
- React ref的用法React
- js中的this用法JS
- async的基本用法
- dva的基本用法
- rematch的基本用法REM
- Generator的基本用法
- Phaser的timer用法
- 指標的用法指標
- webpack的基本用法Web
- 【WPF】Grid的用法
- meta元素的用法
- PHP Session的用法PHPSession
- JS Object的用法JSObject
- type[xxx]的用法
- extern "c"的用法
- fortran continue的用法
- Ruby Module的用法
- 棧——stack的用法