简体   繁体   English

2种不同的型号-如何在MVC 5中连接它们?

[英]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. 对于我的Web应用程序项目,我有2个彼此相关的模型。 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; }
    }

I think you can create a EmployeeViewModel. 我认为您可以创建一个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. 单击添加新员工按钮时,只需设置所选的DepartmentId = DepartmentId。 Or you can let the user changes Deparment. 或者,您可以让用户更改Deparment。

You can create a ViewModel like this. 您可以像这样创建一个ViewModel。 Whenever you want to show employee details in the view then just map your data to this view model along with "DepartmentID" and "DepartmentName". 每当您想在视图中显示员工详细信息时,只需将数据以及“ DepartmentID”和“ DepartmentName”一起映射到该视图模型即可。 To get "DepartmentName" you can join with department table and get department name. 要获取“ DepartmentName”,您可以与部门表联接并获取部门名称。

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. 要获得部门名称,您可以像这样与Employe表一起加入。 (Note that I've used EntityFramework here to retrieve data) (请注意,我在这里使用EntityFramework来检索数据)

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
            };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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