Vy Nguyen
Vy Nguyen

Reputation: 57

2 different models - how to connect them in MVC 5?

For my web app project, I have 2 models that are related to each other. Model Department is related to Model Employee. Each employee is assigned one department, while each department can have many employees. In the Departments view, I have an "Add new employee" option. When the add new employee button is clicked, a modal popup comes up which shows the Employees/Create view. My problem is I don't know how to link employee to department so that the employee automatically gets added to the department view next to the right department.

Right now, my Employee/Create view just gives the user a dropdown list of Departments to link the employee to. I want the employee to be automatically linked to the department when the "add employee" option is shown in the Departments view.

Here's the Department model:

public class Department
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public string BuildingLocation { get; set; }
    public string DirectLine { get; set; }
    public virtual ICollection<Employee> Employees { get; set; }

}

Here's the Employee model:

 public class Employee
    {
        [Key]
        public int EmployeeID { get; set; }
        [ForeignKey("Department")]
        public int DepartmentID { get; set; }
        public string EmployeeFirstName { get; set; }
        public string EmployeePosition { get; set; }
        public string EmployeePhoneNo { get; set; }
        public string EmployeeEmail { get; set; }
        public virtual Department Department { get; set; }
    }

Upvotes: 0

Views: 263

Answers (2)

Sheikh Kawser
Sheikh Kawser

Reputation: 136

You can create a ViewModel like this. Whenever you want to show employee details in the view then just map your data to this view model along with "DepartmentID" and "DepartmentName". To get "DepartmentName" you can join with department table and get department name.

public class EmployeeViewModel
{
    public int EmployeeID { get; set; }
    public string EmployeeFirstName { get; set; }
    public string EmployeePosition { get; set; }
    public string EmployeePhoneNo { get; set; }
    public string EmployeeEmail { get; set; }

    public int DepartmentID { get; set; }
    public string DepartmentName { get; set; }
    public virtual Department Department { get; set; }
}

To Get Department Name you can join with Employe table like this. (Note that I've used EntityFramework here to retrieve data)

var employeeList = from e in dbContext.Employees
            join d in dbContext.Departments on e.DepartmentID equals d.DepartmentID
            select new EmployeeViewModel
            {
                EmployeeID = e.EmployeeID,
                EmployeeFirstName = e.EmployeeFirstName,
                EmployeePosition = e.EmployeePosition,
                EmployeePhoneNo = e.EmployeePhoneNo,
                EmployeeEmail = e.EmployeeEmail,
                EmployeePhoneNo = e.Name,

                DepartmentID = e.DepartmentID,
                DepartmentName = d.DepartmentName
            };

Upvotes: 0

Tomato32
Tomato32

Reputation: 2245

I think you can create a EmployeeViewModel. For example:

public class Employee
    {        
        public int EmployeeID { get; set; }        
        public int DepartmentID { get; set; }
        public string EmployeeFirstName { get; set; }
        public string EmployeePosition { get; set; }
        public string EmployeePhoneNo { get; set; }
        public string EmployeeEmail { get; set; }
        public SelectListItem DepartmentList { get; set; }        
    }

When you click button add new employee, just set DepartmentId = DepartmentId that you selected. Or you can let the user changes Deparment.

Upvotes: 1

Related Questions