1
我的視圖包含下拉列表。填充下拉列表代碼:獲取下拉列表控制器中的選定值
public ActionResult Create()
{
var architects = (from ar in db.ArchitectMasters where ar.IsActive == 1 select ar).ToList();
ProjectMaster ps = new ProjectMaster();
ViewBag.ArchitectID = new SelectList(architects, "ArchitectID", "ArchitectName",ps.ArchitectID);
return View();
}
查看代碼:
<div class="editor-label">
@Html.Label("Architect")
</div>
<div class="editor-field">
@Html.Hidden("ArchitectID")
@Html.DropDownList("ArchitectID ", (IEnumerable<SelectListItem>)ViewBag.ArchitectID)
</div>
後創建方法:
public ActionResult Create(ProjectMaster projectmaster, long ArchitectID, FormCollection data)
{
if (ModelState.IsValid)
{
//string dropDownSelectedValue = data.Get("ArchitectName");
//ViewBag.ArchitectID = data["ArchitectName"];
long architectname = long.Parse(ArchitectID.ToString());
db.ProjectMasters.Add(projectmaster);
db.SaveChanges();
return RedirectToAction("Index");
}
}
我如何在我的控制器選擇下拉列表的價值?請幫忙。