我希望创建一个处理父子对象关系的向导或多步骤表单。第 1 步创建父对象,然后第 2 步创建第一个子对象。
楷模:
public class Company
{
[Key]
public int ID{get;set;}
public string CompanyName{get;set;}
public virtual List<Employee> Employees{get;set;}
}
public class Employee
{
[Key]
public int ID{get;set;}
public string EmployeeName{get;set;}
[ForeignKey("CompanyID")]
public Company EmployeeCompany{get;set;}
public int CompanyID {get;set;}
}
目标是创建一个两步表单,首先获取公司信息,然后获取员工信息。我将如何设置我的控制器来处理这个问题?我是否为公司设置了两个操作结果,为员工设置了两个操作结果。其中一个 actionresult 返回一个空白/空表单,而第二个 actionresult 处理 HttpPost actionresult。
ActionResult CreateCompany()
{
//Send back a blank view to create the company
return View();
}
[HttpPost]
ActionResult CreateCompany(Company comp)
{
//Check comp.id to determine if it is add or update then save changes via entity framework
//Use newly created ID from database for next step
Return CreateEmployee(comp.id)
}
ActionResult CreateEmployee(int companyid)
{
//This is where I get stuck
//Do I create a new employee object and set the companyid
return View(); //Blank CreateEmployee View
}
[HttpPost]
ActionResult CreateEmployee(Employee employee)
{
//Add employee to company and save the company via entity framework
//Send to next page or next step of wizard
}
我最好为此使用部分视图吗?也许做一些 jquery/ajax 来发布创建公司并显示创建员工?