【问题标题】:How to get value from dropdown to insert in EF CodeFirst如何从下拉列表中获取值以插入 EF Code First
【发布时间】:2016-09-28 08:12:49
【问题描述】:

我正在开发 Webforms(没有 MVC)我有以下场景。我想获取下拉列表的选定文本的 ID 并在单击事件时插入。但它显示从字符串到 EFCode.Department 的转换错误。

代码

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; }
    }

【问题讨论】:

  • 好吧Department 不是字符串,它是一个。查看您的代码。
  • 这是我的问题。如何将字符串转换为类类型
  • @S.Akbari Department 是导航属性。如何在员工对象中设置它,从下拉列表中检索其值。
  • 使用对象初始化器,就像您已经为 Employee 所做的一样。例如:Department = new Department { ID = ....,Name = DropDownList1.SelectedValue, }
  • @S.Akbari thisn 将添加一个新值,而不是获取数据库中选定项目的值。我只需要选定项目的值,它已经在数据库中。

标签: c# asp.net entity-framework webforms code-first


【解决方案1】:

您不能使用字符串替换部门对象 当您执行代码时,关系将为 int ,因此您必须使用转换为 Int32

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

【讨论】:

  • 这会抛出一个错误,不能隐式转换转换类型 int 到 Project.Department
【解决方案2】:
Employee emp = new Employee(){Name = TextBox1.Text,
            Gender= TextBox2.Text,
            Salary = float.Parse(TextBox3.Text),
            Department = db.FirstOrDefault(x => x.Name == DropDownList1.SelectedValue)};

【讨论】:

  • 问题依然存在
【解决方案3】:

您不能将部门声明为属性。我已经修改了您的代码,以便使用 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();
       }

【讨论】:

  • 即 s.Name 我现在已经修改了,你的 Department 表的列名..这是你的要求吗?
【解决方案4】:

您的问题在于页面加载

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:(Convert.ToInt32(Dropdownlist.SelectedValue))

【讨论】:

    猜你喜欢
    • 2014-10-30
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    相关资源
    最近更新 更多