我正在尝试使用下拉列表向数据库插入值。下拉列表中的值可以将完成的所有数据插入数据库。但它仍然在View中警告错误。
值不能为空
@Html.DropDownListFor(Model => Model.JCY_SEC, new SelectList(ViewBag.Names as System.Collections.IEnumerable, "SECID", "SecTH"), "Select a Section")
这是我的控制器
[HttpGet]
public ActionResult EmployeeView()
{
ViewBag.Names = db.HRset_Section.ToList();
return View();
}
这是我的插入方法
public ActionResult EmployeeView(EMPJCYMODEL customer, FormCollection collection)
{
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "INSERT INTO HR_EMPLOYEE(JCY_EMPNO, JCY_EMP_NAME,JCY_EMP_SUR,st_date,JCY_SEC) VALUES(@JCY_EMPNO, @JCY_EMP_NAME,@JCY_EMP_SUR,@st_date,@JCY_SEC)";
query += " SELECT SCOPE_IDENTITY()";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@JCY_EMPNO", customer.JCY_EMPNO);
cmd.Parameters.AddWithValue("@JCY_EMP_NAME", customer.JCY_EMP_NAME);
cmd.Parameters.AddWithValue("@JCY_EMP_SUR", customer.JCY_EMP_SUR);
cmd.Parameters.AddWithValue("@st_date", customer.st_date);
cmd.Parameters.AddWithValue("@JCY_SEC", customer.JCY_SEC);
customer.JCY_EMPNO = cmd.ExecuteScalar().ToString();
con.Close();
}
}
return View(customer);
}