开发者

Drop Down List in MVC C#

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜