Asp.net mvc MVC——在Northwind DB中创建控制器操作CreateNewEmployee
我尝试在MVC中创建一名新员工。 控制器:Asp.net mvc MVC——在Northwind DB中创建控制器操作CreateNewEmployee,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我尝试在MVC中创建一名新员工。 控制器: HireEmployeeBL.EmployeeBL employeeBl = new HireEmployeeBL.EmployeeBL(); public ActionResult HireNew(int id) { return View(); } [HttpPost] public ActionResult HireNew(int id, Employee employee) { employee.ReportsTo
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”为空
方法的不可为Null的类型“System.Int32”
中的“System.Web.Mvc.ActionResult HireNew(Int32)”
“MvcEmployee.Controllers.EmployeeController”。可选参数
必须是引用类型、可为null的类型或声明为
可选参数。参数名称:参数
您是否将
int
类型的id和employee
类型的员工都传递给您的控制器操作?我已经编辑了您的标题。请看,“,其中的共识是”不,他们不应该“。是的,我把它们都传递给了控制员。你的观点/形式是什么样的?顺便说一句,你没有通过他们两个。。。我不是想粗鲁,但事实是,如果你传递的是'id'参数,你就不会得到那个错误。我忘了添加“HireEmployeeBL.EmployeeBL EmployeeBL=new HireEmployeeBL.EmployeeBL();”我使用EmployeeBL而不是northwindEntities,你必须调用-northwindEntities.SaveChanges();-实际上,您应该将其提取到模型中,并尽可能精简控制器中的逻辑。
[HttpPost]
public ActionResult Create(Employee employee)
{
try
{
NorthwindEntities northwindEntities = new NorthwindEntities();
northwindEntities.Employees.Add(employee);
northwindEntities.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}