获取错误是“没有为此对象定义无参数构造函数”。 [英] Get error is "no parameterless constructor defined for this object."

查看:193
本文介绍了获取错误是“没有为此对象定义无参数构造函数”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误:



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.

推荐答案

MVC框架正在创建你的控制器,如果你的控制器在它的构造函数上需要一个参数,那么它需要像这样创建;



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





因为MVC正在创建你的控制器,它作为someEmployeeObject传递了什么?在创建控制器时,MVC假设您拥有所谓的无参数构造函数,这意味着它可以像这样创建它;





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可以创建这个罚款,因为它没有不需要知道任何事情。为了使你的工作,你的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()
{
}





您通常不必将此代码添加到类中,它暗示,但是你自己给了一个构造函数,你不再得到这个隐含的无参数,你需要自己添加它。



这将修复错误但不是你的问题。您的代码仍然需要一个员工对象,那么它来自哪里?我们不知道您的系统,所以无法回答这个问题。您可能需要在无参数构造函数中自己创建它。



另一种可能性是您尝试使用IoC容器但尚未注册它。要有一个没有无参数构造函数的控制器,你需要使用



注册你的IoC DependencyResolver.SetResolver(yourIoCContainer) [ ^ ]



你需要在那个容器中注册IEmployee这样MVC框架知道如何将IEmployee解析为可以传递给控制器​​的具体对象。



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.


只需要在代码上添加默认构造函数。



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%


当您收到错误消息时,请将其提供给Google:没有为此对象定义的无参数构造函数 - Google搜索 [ ^ ]

它让成千上万的人提出完全相同的问题,并得到答案:MVC模型必须有一个无参数构造函数。

添加:

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


这篇关于获取错误是“没有为此对象定义无参数构造函数”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆