【发布时间】:2015-03-11 11:04:19
【问题描述】:
我想要一个下拉列表,它从资格表中提取数据,然后在 HttpPost 上,它将下拉列表中选择的值存储到员工表上的“资格”列中。我对 MVC 很陌生,我不太了解语法。我也不知道如何使用 jquery、vb 等。很抱歉,如果有一篇文章涵盖了这个,我没有看到它,因为我不知道我在看什么。
这是我目前最好的尝试:
控制器:
private Database1Entities db = new Database1Entities();
...
...
...
// GET: /Home/CreateEmp
public ActionResult CreateEmp()
{
ViewBag.QualList = new SelectList(db.Qualifications, "Id", "qual");
return View();
}
// POST: /Home/CreateEmployee
[HttpPost, ActionName("CreateEmployee")]
public ActionResult CreateEmpResult(Employee emp)
{
if (ModelState.IsValid)
{
db.Employees.Add(emp);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.QualList = new SelectList(db.Qualifications, "Id", "qual", emp.qualification);
return View(emp);
}
创建员工视图:
@model MvcDropDownList.Models.Employee
@using (Html.BeginForm("CreateEmployee", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<table>
<!-- other fields removed for sake of simplicity -->
<tr>
<td>
@Html.Label("Select Your Qualification:")
</td>
<td>
@Html.DropDownList("QualList", "--Select--")
</td>
</tr>
</table>
}
表结构:
资格表:Id、qual
员工表:ID、名字、姓氏、性别、资格
我的代码成功从“资格”表中获取数据,但没有将其发布到“员工”表中的“资格”列。
任何建议将不胜感激,谢谢!
【问题讨论】:
标签: c# asp.net-mvc model-view-controller drop-down-menu