Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got the error:

C#
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.




Code Base :
C#
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();
        }


What I have tried:

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. " so I don't understand how to find out the solution and where is the issue.
Posted
Updated 5-May-20 5:48am
Comments
Patrice T 27-Mar-16 3:54am    
"Please review the stack trace for more information about the error and where it originated in the code."
And it says ?
PUSHPAM KUMAR AWASTHI 1-Feb-23 6:55am    
No parameterless constructor defined.
get me the solution of it.

The MVC framework is creating your controller, if your controller needs a parameter on its constructor then it needs to be created like this;

C#
EmployeeController c = new EmployeeController(someEmployeeObject);


as MVC is creating your controller, what does it pass as someEmployeeObject? When creating controllers MVC assumes you have what is known as a parameterless constructor which means it can create it like this;

C#
EmployeeController c = new EmployeeController();


MVC can create this fine as it doesn't need to know anything. For that to work your EmployeeController needs a parameterless constructor

C#
public EmployeeController()
{
}


You don't normally have to add this code to a class, it is implied, but as you have given a constructor yourself you no longer get this implied parameterless one, you need to add that yourself.

That will fix the error but not your problem. Your code still expects an employee object so where does that come from? We don't know your system so can't answer that. You might need to create it yourself in the parameterless constructor.

Another possibility is that you're trying to use an IoC container but haven't registered it. To have a controller with no parameterless constructor you need to register your IoC using

DependencyResolver.SetResolver(yourIoCContainer)[^]

and you need to register IEmployee with that container so that the MVC framework knows how to resolve IEmployee to a concrete object that it can pass to your controller.
 
Share this answer
 
Just require to add default constructor on your code.

C#
namespace MVCSampleApplication.Controllers
{
    public class EmployeeController : Controller
    {
//Added Default Constructor
    public EmployeeController(){}
        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();
        }
That will work 100%
 
Share this answer
 
v2
When you get an error message you don't understand, feed it to Google: No parameterless constructor defined for this object - Google Search[^]
It gives thousands of others asking exactly the same question, and getting the answer: MVC models must have a parameterless constructor.
Add this:
C#
public EmployeeController()
{
}
 
Share this answer
 
Hi Pramod,

Please follow the below steps. As I understood from the issue you are trying to use Dependency Injection and you are not registering for dependency Injection. This happened to me as well and adding below, I was able to fix.

1) If you are using Unity, please make sure below is added in unityConfig.cs (under App_start)
C#
container.RegisterType<IEmployee, <<class name where you inherit the interface>>();


for eg in my case I had IEmployeeRepository Interface and EmployeeRepository Class. This is how I Use it

C#
container.RegisterType<IEmployeeRepository, EmployeeRepository>();


C#
IEmployeeRepository.cs
    public interface IEmployeeRepository
        {
            List<Employee> GetAllEmployees();
            Employee GetEmployeeByID(int ID);
    
        }


C#
EmployeeRepository.cs
    public class EmployeeRepository : IEmployeeRepository
        {
            private readonly EmployeeEntities _employeeEntities;
    
            public EmployeeRepository(EmployeeEntities employeeEntities)
            {
                _employeeEntities = employeeEntities;
            }
            public List<Employee> GetAllEmployees()
            {
                return _employeeEntities.Employees.ToList();
            }
    
            public Employee GetEmployeeByID(int ID)
            {
                var employee = _employeeEntities.Employees.Where(e => e.EmpNo == ID).FirstOrDefault();
                return employee;
            }
        }
    }


2) In Global.asax, please register unity like below
C#
UnityConfig.RegisterComponents();


I have seen people asking to add default controller. if you are using default controller the dependency Injection won't work. and you have to use 'new' keyword which is strongly typed. This also gives you null reference as the IEmployee is not initialized with anything.

Hope this helps!! Happy Coding!!
 
Share this answer
 
v2
Comments
phil.o 5-May-20 12:39pm    
The error suggests that it is in an ASP.NET project, so this has nothing to do with Unity. Moreover, this has been asked and marked as answered more than four years ago.
Member 15546027 23-Feb-22 21:01pm    
Thanks to Sanjeev Jyothikrishnan
None of the above solutions solved my error but yours worked for me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900