I am trying to retrieve a value from a drop down list in MVC, I am able to bind the drop down list from a table but unable to save the id of the drop down value selected back to the database.
//The following is a snippet of my Create.aspx
<%= Html.dropdownlist("departments", "Select One")%>
//The following is a snippet of my HomeController.cs
public ActionResult Create()
{
this.ViewData["departments"] = new SelectList(_service.ListDepartments(), "departmentID", "name");
return View("Create");
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(int? deptID, [Bind(Exclude = "educationID")] tblEDA empToCreate)
{
if (_service.CreateEmp(deptID, empToCreate))
return RedirectToAction("Index");
return View("Create");
}
Any help would really be appreciated.
Change your
deptIDargument name todepartments.Arguments in MVC are matched to post/route data. Because you are creating a DropDownList with the name “departments”, that is the name of the form element. When the data is submitted, there is no data named
deptID, onlydepartments.I’d also make
deptIDanint(as apposed toint?) unless it’s actually optional.