2016-05-30 109 views
0

我正在从事Webforms(无MVC)我有以下情形。我想要获取所选文字的ID并插入点击事件。但它显示从字符串到EFCode.Department的转换错误。如何从下拉列表中获取值以插入EF CodeFirst

代码

EmployeeDbContext db = new EmployeeDbContext(); 
     protected void Page_Load(object sender, EventArgs e) 
     { 

      if (!IsPostBack) 
      { 
       DropDownList1.DataSource = db.Departments.ToList(); 
       DropDownList1.DataBind(); 
      } 


      GridView1.DataSource = db.Departments.Include("Employees").ToList(); 
      GridView1.DataBind(); 



     } 

     protected void Button1_Click(object sender, EventArgs e) 
    { 
     Employee emp = new Employee(){Name = TextBox1.Text, 
      Gender= TextBox2.Text, 
      Salary = float.Parse(TextBox3.Text), 
      **Department = DropDownList1.SelectedValue**}; // error here 

     db.Employees.Add() 
      db.SaveChanges(); 
    } 

public class Department 
    { 
     public int ID { get; set; } 
     public string Name { get; set; } 
     public string Location { get; set; } 
     public List<Employee> Employees { get; set; } 
    } 


    public class Employee 
    { 
     public int ID { get; set; } 
     public string Name { get; set; } 
     public string Gender { get; set; } 
     public float Salary { get; set; } 
     **public Department Department { get; set; }** 
    } 




    public class EmployeeDbContext : DbContext 
    { 
     public DbSet<Employee> Employees { get; set; } 
     public DbSet<Department> Departments { get; set; } 
    } 
+0

Well'Department'不是一个字符串,它是一个**类**。查看你的代码。 –

+0

这是我的问题。如何将字符串转换为类类型 – Mangrio

+0

@ S.Akbari“Department”是导航属性。如何在员工对象中设置它,从下拉菜单中重新获得它的价值。 – Mangrio

回答

0

当你执行代码的关系将是int,在那里你必须使用转换的Int32

Employee emp = new Employee(){Name = TextBox1.Text, 
     Gender= TextBox2.Text, 
     Salary = float.Parse(TextBox3.Text), 
     Department = db.GetDepartmentByID(Convert.ToInt32(DropDownList1.SelectedValue))}; 
+0

这抛出一个错误,不能转换隐式转换类型int到Project.Department – Mangrio

0

你不能申报部门不能使用字符串替代部门的对象 作为一个属性..我已经修改你的代码的方式,将通过使用dropDownList1.SelectValue从您的部门表中取回名称。不确定这是否是您的要求..

代码:

 protected void Button1_Click(object sender, EventArgs e) 
     { 
     Employee emp = new Employee(){ 
     Name = TextBox1.Text, 
     Gender= TextBox2.Text, 
     Salary = float.Parse(TextBox3.Text), 
      Department = new Department() 
      { 
       Name = db.Department.Where(s => s.Id == dropDownList1.SelectValue).Select(s.Name).FirstOrDefault(); 

      }, 
     }; 
     db.Employees.Add(); 
     db.SaveChanges(); 
     } 
+0

即,s.Name我已经修改,现在你的部门表的列名..是你的要求? –

0
Employee emp = new Employee(){Name = TextBox1.Text, 
      Gender= TextBox2.Text, 
      Salary = float.Parse(TextBox3.Text), 
      Department = db.FirstOrDefault(x => x.Name == DropDownList1.SelectedValue)}; 
+0

仍然存在问题 – Mangrio

0

你的问题是在页面加载

if (!IsPostBack) 
      { 
       DropDownList1.DataSource = db.Departments.ToList(); 
       DropDownList1.DataBind(); 
      } 

您需要添加此

if (!IsPostBack) 
       { 
        DropDownList1.DataSource = db.Departments.ToList(); 
        DropDownList1.DataTextField = "Name"; 
        DropDownList1.DataValueField = "ID"; **//This will get the id** 
        DropDownList1.DataBind(); 
       } 

也转换为INT:(转换。 ToInt32(Dropdownlist.SelectedValue))

相关问题