Reputation: 388
I am unable to get value of id form dynamic dropdownlist using viewbag, I am using a stored procedure to insert the data. The value of id is coming from the dynamic dropdown.
In my view I am using
@Html.DropDownList("DepartmentID", new SelectList(ViewBag.data, "depId", "depId"), "Department-ID")
Whereas in my controller
public ActionResult Create()
{
var Dictionary = db.departments.ToList();
if (Dictionary != null)
{
ViewBag.data = Dictionary;
}
return View();
}
[HttpPost, ActionName("Create")]
public ActionResult CreateThroughStoreProcedure(employee emp)
{
if (ModelState.IsValid)
{
db.ShowdDetails(emp.name, emp.city, emp.gender, emp.depId);
db.SaveChanges();
return RedirectToAction("Index");
}
return View("Error");
}
Upvotes: 1
Views: 502
Reputation: 219127
You've named your UI field DepartmentID
:
@Html.DropDownList("DepartmentID", new SelectList(ViewBag.data, "depId", "depId"), "Department-ID")
But the property on your model is depId
. Your field names need to match the property names of the model to which you're binding them:
@Html.DropDownList("depId", new SelectList(ViewBag.data, "depId", "depId"), "Department-ID")
Upvotes: 2