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()方法的基本用法,可以根据需要进行参数的调整和扩展。