Html.DropDownList()是ASP.NET MVC框架中的HTML助手方法,用于生成下拉列表(DropDownList)的HTML代碼。
語法:
public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, string optionLabel, object htmlAttributes)
參數說明:
IEnumerable<SelectListItem>
,其中SelectListItem
表示下拉列表中的每個選項。示例:
@Html.DropDownList("Country", ViewBag.CountryList as SelectList)
@Html.DropDownList("Country", ViewBag.CountryList as SelectList, "Select a Country")
@Html.DropDownList("Country", ViewBag.CountryList as SelectList, new { @class = "form-control", onchange = "countryChanged()" })
ViewBag.CountryList = new SelectList(new List<string> { "USA", "Canada", "UK", "Australia" });
以上是Html.DropDownList()方法的基本用法,可以根據需要進行參數的調整和擴展。