我希望创建一个处理父子对象关系的向导或多步骤表单。步骤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;}
}
目标是创建一个两步形式,首先获取公司信息,然后转到员工信息。我如何设置我的控制器来处理这个?我是否为公司设置了两个Action结果,为Employee设置了两个Action结果。其中一个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来发布创建公司和show create employee?