Click here to Skip to main content
14,360,151 members
Rate this:
Please Sign up or sign in to vote.
See more:
I got the error:

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 :
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 8-Nov-19 6:44am
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 ?
Rate this:
Please Sign up or sign in to vote.

Solution 2

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

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;

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

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Just require to add default constructor on your code.

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%
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

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:
public EmployeeController()
{
}
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100