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;
相關文章
- @html.DropDownList關於它的使用HTML
- Qdrant用法;Qdrant在langchain裡的用法LangChain
- SQL AS 的用法SQL
- SUBMIT 的用法MIT
- indexOf()的用法Index
- Promise的用法Promise
- PHPdefine()的用法PHP
- SqlParameter的用法SQL
- Tcpdump 的用法TCP
- Javascript 的 this 用法JavaScript
- top的用法
- iptables的用法
- tie的用法
- SqlServer的with(nolock)的用法SQLServer
- Oracle的Cast的用法OracleAST
- jQuery的:checked的用法jQuery
- PHP Session的用法PHPSession
- meta元素的用法
- React Hooks 的用法ReactHook
- MongoDB的基本用法MongoDB
- React ref的用法React
- webpack的基本用法Web
- rematch的基本用法REM
- Pythonyield的用法Python
- Vue slot的用法Vue
- Mysql LIMIT的用法MySqlMIT
- Promise的基本用法Promise
- js中的this用法JS
- Seajs的用法JS
- *html的CSSHacks用法HTMLCSS
- java Properties的用法Java
- golang range的用法Golang
- 【SqlServer系列】AS的用法SQLServer
- Oracle Hints的用法Oracle
- oracle job的用法Oracle
- OpenCV的SVM用法OpenCV
- Oracle keep的用法Oracle
- SQL 中With as 的用法SQL