tags:

views:

76

answers:

1

Hello,

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. Thank you.

+3  A: 

Change your deptID argument name to departments.

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, only departments.

I'd also make deptID an int (as apposed to int?) unless it's actually optional.

Richard Szalay
Thank you for your help Richard, that worked!
GB