我的视图包含下拉列表。填充下拉列表代码:
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");
}
}
如何在我的控制器中获取下拉列表的选定值?请帮忙。