-1

I have this constructor that return list of surveys in database

public class SurveysController : Controller
{
    private readonly ApplicationDbContext _db;
    // GET: Survey/Surveys
     public SurveysController(ApplicationDbContext db)
    {
        _db = db;
    }
    [HttpGet]
    public ActionResult Index()
    {
        var surveys = _db.Surveys.ToList();
        return View(surveys);
    }
}

but it gives me error that *No parameterless constructor defined for this object. * Any Help ?!

ali
  • 75
  • 6

1 Answers1

1

The exception is straightforward: you need a parameterless constructor for the controller class, because you have a constructor with one parameter like this:

public SurveysController(ApplicationDbContext db)
{
    _db = db;
}

Therefore you should add parameterless constructor like this:

public SurveysController()
{
    // do something
}

Related issues:

MVC ASP.NET No parameterless constructor defined for this object

No parameterless constructor defined for this object. in ASP.NET MVC Controller

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61