[英]Drop Down List in MVC C#
我試圖從MVC中的下拉列表中檢索一個值,我能夠從表中綁定下拉列表但無法將選擇的下拉值的id保存回數據庫。
//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");
}
真的很感激任何幫助。
將deptID
參數名稱更改為departments
。
MVC中的參數與后期/路由數據匹配。 因為您正在創建名為“departments”的DropDownList,所以這是表單元素的名稱。 提交數據時,沒有名為deptID
數據,只有departments
。
我還讓deptID
的int
(為並列於int?
),除非它實際上是可選的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.