Hussain Hashimi

Hussain Hashimi

  • NA
  • 8
  • 2.2k

MVC with Entity Framework (One to Many relationship)

Jun 14 2019 5:17 AM
Hi,
I am new to MVC, I am trying to create "code first" mvc form and have created two classes as follows.
public class Department
{
public int DepartmentId { get; set; }
[Required()]
[StringLength(150, MinimumLength =10)]
public String DepartmentName { get; set; }
public virtual List<Employee> Employee { get; set; }
}
public class Employee
{
public int EmployeeId { get; set; }
[Required()]
[StringLength(150, MinimumLength =4)]
public String FName { get; set; }
[Required()]
[StringLength(150, MinimumLength = 4)]
public String LName { get; set; }
public virtual Department Department { get; set; }
}
Now, when I create a controller for these classes, and it creates the View automatically, I get the input / details / delete etc automatically.
But when I go to add new employee page, it displays the field's for First name and Last name, but doesn't display the selection for the department.
What I want to do ideally is:
Step1: Go to Departments and Add a couple of departments (say department 1, department 2, department 3 ...)
Step2: Go to employee page and add employee (enter employee first name, last name, and select the department from a drop down list)
so that question is how to display the drop down list for the departments, if it is not automatically created.
thank you .

Answers (1)