MVC - 在Northwind DB中创建Controller Action CreateNewEmployee

时间:2013-04-10 18:00:35

标签: asp.net-mvc asp.net-mvc-4

我尝试在MVC中创建一个新的Employee。 控制器:

HireEmployeeBL.EmployeeBL employeeBl = new HireEmployeeBL.EmployeeBL();

    public ActionResult HireNew(int id)
{
    return View();
}

[HttpPost]
public ActionResult HireNew(int id, Employee employee)
{
    employee.ReportsTo = new Manager { EmployeeID = id };
    employeeBl.AddEmployee(employee);
    return RedirectToAction("Subordinates");
    //return View();
}

服务器错误:

  

参数字典包含参数'id'的空条目   方法的非可空类型'System.Int32'   'System.Web.Mvc.ActionResult HireNew(Int32)'中   'MvcEmployee.Controllers.EmployeeController'。可选参数   必须是引用类型,可空类型,或声明为   可选参数。参数名称:参数

1 个答案:

答案 0 :(得分:0)

   [HttpPost]
        public ActionResult Create(Employee employee)
        {
            try
            {
                NorthwindEntities northwindEntities = new NorthwindEntities();
                northwindEntities.Employees.Add(employee);
                northwindEntities.SaveChanges(); 

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }