0
votes

I set the E_id as a primary key.My Edit function is not working.It continuously throws the error.But my create and index functions working good.What will be the problem in my code?This is the ERROR

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

public ActionResult Index()
        {

            EmployeeBusinessLayer employeebusinesslayer = new EmployeeBusinessLayer();
            List<Employee> employees = employeebusinesslayer.Employees.ToList();

            return View(employees);
        }

        [HttpGet] //Create after this Get
        [ActionName("Create")] //old name of function
        public ActionResult Create_get()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            EmployeeBusinessLayer employeebusinesslayer = new EmployeeBusinessLayer();

                Employee employee = employeebusinesslayer.Employees.Single(emp => emp.E_id == id);

                return View(employee);

        }

        [HttpPost] //Create after this Get
        [ActionName("Create")]//old name of function 
        public ActionResult Create_post() 
        {
            if (ModelState.IsValid)
            {
                Employee employee = new Employee();
                UpdateModel(employee); //Updatemodel for employee - Call for new entries

                EmployeeBusinessLayer employeebusinesslayer = new EmployeeBusinessLayer();
                employeebusinesslayer.AddEmployee(employee);

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

    }
}
1

1 Answers

1
votes

You should change Edit(int id) to Edit(int? id) to prevent case input id empty in URL or not number.

And update this line of code

Employee employee = id != null ? employeebusinesslayer.Employees.Single(emp => emp.E_id == id.Value) : null;