2013-04-10 39 views
0

我尝试在MVC中创建一个新的Employee。 控制器:MVC - 在Northwind DB中创建控制器操作CreateNewEmployee

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(); 
} 

服务器错误:

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult HireNew(Int32)' in 'MvcEmployee.Controllers.EmployeeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

+0

你路过这两个类型'int'的ID和类型Employee'的'员工到控制器的动作? – 2013-04-10 18:12:55

+1

我编辑了你的标题。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。 – 2013-04-10 18:26:40

+0

是的,即时通过他们两人控制器行动 – 2013-04-10 18:38:35

回答

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

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

我忘了添加“HireEmployeeBL.EmployeeBL employeeBl = new HireEmployeeBL.EmployeeBL();”我使用employeeBl而不是NorthwindEntities – 2013-04-10 18:32:20

+0

,你必须调用 - northwindEntities.SaveChanges(); - 实际上将其保存在数据库中。 – marko 2013-04-12 05:10:35

+0

实际上,您应该将其提取到模型中,并将控制器内部的逻辑保持尽可能薄。 – marko 2013-04-12 05:11:47