ASP.NET MVC 视图里面的下拉框怎么动态显示数据库里的数据??
2个回答
2012-09-22
展开全部
public ActionResult TestDropDownList()
{
List<SelectListItem> lists = new List<SelectListItem>();
lists.Add(new SelectListItem { Text = "001", Value = "111" });
lists.Add(new SelectListItem { Text = "002", Value = "222" });
lists.Add(new SelectListItem { Text = "003", Value = "333" });
lists.Add(new SelectListItem { Text = "004", Value = "444" });
ViewData["lists"] = lists;
ViewData["selected"] = 444;
ViewData["text"] = "CHLH";
return View();
}
========================
下拉框:
@Html.DropDownList("lists")
<br />
@Html.DropDownList("selected",ViewData["lists"] as IEnumerable<SelectListItem>)
================
关键还是lists吧!~
{
List<SelectListItem> lists = new List<SelectListItem>();
lists.Add(new SelectListItem { Text = "001", Value = "111" });
lists.Add(new SelectListItem { Text = "002", Value = "222" });
lists.Add(new SelectListItem { Text = "003", Value = "333" });
lists.Add(new SelectListItem { Text = "004", Value = "444" });
ViewData["lists"] = lists;
ViewData["selected"] = 444;
ViewData["text"] = "CHLH";
return View();
}
========================
下拉框:
@Html.DropDownList("lists")
<br />
@Html.DropDownList("selected",ViewData["lists"] as IEnumerable<SelectListItem>)
================
关键还是lists吧!~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询