简体   繁体   中英

How to get the value of selected item and insert

I have made this simple code to insert a record through Entity Framework in Web Form not in MVC . I want to insert the department value from selected item from DropdownList 在此处输入图片说明

above data is manually added in database... If I am inserting record from here.. it is creating new slot of data. I want that to be added inside the same category which is selected from dropdown.

Code

public partial class WebForm1 : System.Web.UI.Page
{
    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),

            //this retuns '_Page' in ID.
            Department = 
            new Department() { ID = Convert.ToInt32(DropDownList1.SelectedValue)}
        }; 

        db.Employees.Add(emp);
           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; }
}

In this case where the Departments you can choose already exist, you should use and set a FK property instead of set the navigation property:

public class Employee
{
    //add this property
    [ForeignKey("Department")]
    public int  DepartmentId { get; set; }
    public Department Department { get; set; }
}

And then in your code you can do this:

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

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

Setting that property is the only you need to do to relate the current department with the new employee.

Update

Now I saw you are having problems to get the Department Id, in that case you can follow what @Mike recommends in his answer or you can also do this in your page:

<asp:DropDownList ID="DepartmentsDropDownList" runat="server" DataTextField="Name" DataValueField="ID" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM