后台控制器代码 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>