0
votes

I try to create a new Employee in MVC. Controller :

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

Server Error:

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

1
Are you passing both an id of type int and employee of type Employee to your controller action? - Cameron Tinker
I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". - John Saunders
yes im passing both of them to controller Action - LenaT
What does your view / form look like? Btw, you're not passing them both... I'm not trying to be rude, but the fact is, if you were passing an 'id' parameter, your wouldn't be getting that error. - Charlino

1 Answers

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

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