I've been searching online and I cannot find a way to select the chosen department when a user wants to update the employee.
I want the selected value obj.Departmentto define the order of the selectList listItems I can send everything else except the department.
public IActionResult EditDetail(Employee obj)
{
var viewModel = new Employee
{
EmployeeId = obj.EmployeeId,
Name = obj.Name,
Surname = obj.Surname,
DateOfBirth = obj.DateOfBirth,
Department = obj.Department
};
var listItems = new List<SelectListItem> {
new SelectListItem {Text = "Business", Value = "1"},
new SelectListItem {Text = "Information Technology", Value = "2"},
new SelectListItem {Text = "Human Capital", Value = "3"}
};
ViewBag.listItems = listItems;
return View(viewModel);
}
The html
<select class="form-control" style="width:450px" asp-for="Department" asp-items="ViewBag.listItems">
If you don't have DepartmentId you have to fix Employee class by adding it
action
view