后台代碼:
public ActionResult Index(FormCollection collection) { IList<Project> li = Utility.SqlHelper.getProjectList(); SelectList selec = new SelectList(li, "ID", "Name"); if (collection["drop"] != null) { string projectID = collection["drop"]; selec = new SelectList(li, "ID", "Name", projectID);//根據返回的選中項值設置選中項
ViewData["ruturned"] = collection["drop"]; } ViewData["drop"] = selec; return View(); }
前端代碼:
@using (Html.BeginForm()){
@Html.DropDownList("drop", ViewData["d"] as SelectList)
<input type="submit" value="查看對應分組列表" />
}
<p> 當前項目ID: @ViewData["ruturned"]</p>