NET MVC提供了5种DropDownListFor扩展方法。
- public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList);
- ----------------------------------------------------------------------------------------
- public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, IDictionary<string, object> htmlAttributes);
- ----------------------------------------------------------------------------------------
- public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, object htmlAttributes);
- ----------------------------------------------------------------------------------------
- public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel);
- ----------------------------------------------------------------------------------------
- public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, IDictionary<string, object> htmlAttributes);
DropDownList绑定的数值为列表selectList;
例1:在action中定义selectList
List<SelectListItem> items = new List<SelectListItem>(); items.Add(new SelectListItem { Text = "One", Value = "1" }); items.Add(new SelectListItem { Text = "Two", Value = "2", Selected = true}); this.ViewData["list"] = items;
View中显示
<%=Html.DropDownList("list")%>或<%=Html.DropDownListFor(m=>m.Drp,(SelectList)(ViewData["list"])) %>例2:ViewData["select"] = new SelectList(droData, "DroID", "DroName", Convert.ToInt32(year));