我有两张桌子:tbleducation和tblemployee。 tblemployee表具有以下字段:employeeID,employeeName等。而tbleducation具有以下字段:Id,employeeID等。
我想创建一个dropwdownlist,从tblemployee表中选择一个employeeID给employeeID,以便将其插入到tbleducation中。我编写的代码如下:
模型
public class UserModels
{
public string EmployeeName { get; set; }
public int EmployeeCode { get; set; }
public IEnumerable<SelectListItem> Employee { set; get; }
}
控制器
[HttpPost]
public ActionResult Education(FormCollection edu)
{
tblEmployee_Education education = new tblEmployee_Education();
education.EmployeeID = Convert.ToInt32(edu["EmployeeName"].ToString());
education.Duration = edu["Duration"].ToString();
education.Certificate = edu["Certificate"].ToString();
education.Country = edu["Country"].ToString();
education.SchoolName = edu["SchoolName"].ToString();
education.Major = edu["Major"].ToString();
education.SubDescript = edu["SubDescript"].ToString();
context.AddTotblEmployee_Education(education);
context.SaveChanges();
return View();
}
查看
<%= Html.DropDownListFor(x => x.EmployeeName, Model.Employee, "select EmployeeName")%>
当我尝试插入时,出现以下错误消息:
“INSERT语句与FOREIGN KEY约束FK_tblEmployee_Education_tblEmployee_Employee冲突”。冲突发生在数据库“MP_DBS_old”,表“dbo.tblEmployee_Employee”,列“代码”中。声明已经终止。“
我知道发生此错误是因为dropdownlist中的值是一个字符串,而数据库需要一个整数(employeeID)。我真的不知道如何解决它。任何人都可以帮助我!