ASP.NET MVC HtmlHelper =>Html.DropDownList
用法一:
Controller Action:
为什么为什么
public ActionResult DropDownList() { NorthwindEntities ne = new NorthwindEntities(); var p = ne.Categories; List<SelectListItem> items = new List<SelectListItem>(); foreach(var n in p) { var item = new SelectListItem { Value = n.CategoryID.ToString(), Text = n.CategoryName }; items.Add(item); } ViewData["l"] = items; return View();
View:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>CategoryDropDownList</h2> <%=Html.DropDownList("l") %> </asp:Content>