. Advertisement .
..3..
. Advertisement .
..4..
I am trying to bind model data to my employee view but while I running the application I’m getting error “No parameterless constructor defined for this object.
Error message:
Server Error in '/' Application.
No parameterless constructor defined for this object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.
This is my code:
namespace MVCSampleApplication.Controllers { public class EmployeeController : Controller { IEmployee _IEmployee; public EmployeeController(IEmployee emp) { _IEmployee = emp ; } // GET: Employee public ActionResult Index() { var result = (from e in _IEmployee.GetEmployee() select new Employee { EmpNo = e.EmpNo, EmpName = e.EmpName, Salary = e.Salary, DeptName = e.DeptName, Designation = e.Designation }).ToList(); return View(result); //return View(); }
The cause: I think the cause of this error is that you are trying to use Dependency Injection and you are not registering for dependency Injection.
Solution: You can fix the error by these following steps:
Step 1: Please make sure the following is added to unityConfig.cs (under App start) if you are using Unity.
for eg I have an EmployeeRepository Class and an IEmployeeRepository Interface in my case. How I use it is as follows
Step 2: In Global.asax, please register unity like below