public class DropDownListController : Controller
{
string ConnectionString = string.Empty;
public DropDownListController(IConfiguration configuration)
{
ConnectionString = configuration["ConnectionStrings:DefaultConnection"];
}
public IActionResult Index()
{
return View(new Dropdownlist { Emplist = new SelectList(GetEmployeeList(), "Empid", "EmpName") });
}
[HttpPost]
public IActionResult Index(Dropdownlist dropdownlist)
{
return View(new Dropdownlist { Emplist = new SelectList(GetEmployeeList(), "Empid", "EmpName") });
}
public List<Employee_list> GetEmployeeList()
{
MySqlConnection con = new MySqlConnection(ConnectionString);
MySqlCommand cmd = new MySqlCommand("Select * From Employees", con);
con.Open();
MySqlDataReader idr = cmd.ExecuteReader();
List<Employee_list> Employees = new List<Employee_list>();
if (idr.HasRows)
{
while (idr.Read())
{
Employees.Add(new Employee_list
{
Empid = Convert.ToInt32(idr["Id"]),
EmpName = Convert.ToString(idr["Name"]),
});
}
}
con.Close();
return Employees;
}
}