MVC4.0中下来列表框的,两种使用方法DropDownList
后台控制器代码 public ActionResult Drop() { var list = new List<SchoolInfo>(); list.Add(new SchoolInfo() { SchoolID = 1, SName = "北京大学" }); list.Add(new SchoolInfo() { SchoolID = 2, SName = "上海大学" }); var model = new UserViewInfo(); model.ID = 1; model.Name = "李春城"; model.Age = 13; model.SCID = 1; model.Datas = list; ViewData.Model = model; ViewBag.Drop = new SelectList(model.Datas, "SchoolID", "SName"); return View(); }
前台rasor页面 @{ Layout = null; } @model MVC4Demon.Models.UserViewInfo <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Drop</title> </head> <body> <div> @Model.ID </div> <div>@Model.Name</div>
<!--第一种--> @*<div>@Html.DropDownList("Drop","请选择")</div>*@
<!--第二种--> <div><select id="Drop" name="Drop"> <option id="0">请选择</option> @foreach (var item in Model.Datas) { <option id="@item.SchoolID" selected="@((Model.SCID==item.SchoolID)?"seletectd":"")">@item.SName</option> } </select></div> </body> </html>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。