首页 文章

MVC - 在Northwind DB中创建Controller Action CreateNewEmployee

提问于
浏览
0

我尝试在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();
}

服务器错误:

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

1 回答

  • 0
    [HttpPost]
            public ActionResult Create(Employee employee)
            {
                try
                {
                    NorthwindEntities northwindEntities = new NorthwindEntities();
                    northwindEntities.Employees.Add(employee);
                    northwindEntities.SaveChanges(); 
    
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
    

相关问题