1

I need some help to resolve this problem: I'm still learning how to use MVC; I try to solve a problem wherein an error shows when there are no parameters ( but can not find why: I understand that no parameters are passed but cannot find the point of error; I have created a Data, eManager.Domain, interfaces, but not so as you can pass the parameter only as needed -- the error is:

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.

Data library > DataDepartamentSource.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using eManager.Domain;

namespace Data
{
    public interface DataDepartamentSource
    {
        IQueryable<Employee> Employees { get; }

        IQueryable<Department> Departments { get; }
    }
}

eManager.Domain Library > Department.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace eManager.Domain
{
    public class Department
    {
        [Key]
        public virtual int IdDep { get; set; }
        public virtual string NameDep { get; set; }
        public virtual ICollection<Employee> Employees{ get; set; }
    }
}

eManager.Domain Library > Employee.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace eManager.Domain
{
    public class Employee
    {
        [Key]
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
    }
}

On the website Principal Web:

Infrastructure > DepartmentDataBase.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace eManager.Domain
{
    public class Employee
    {
        [Key]
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
    }
}

Migrations > Configuration.cs:

namespace PluralSight_MVC_Ejercicio.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<PluralSight_MVC_Ejercicio.Infrastructure.DepartmentDataBase>
    {

        //public DepartmentDB() : base("DefaultConnection")
        //{

        //}

        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }

        protected override void Seed(PluralSight_MVC_Ejercicio.Infrastructure.DepartmentDataBase context)
        {

            context.department.AddOrUpdate(m => m.NameDep,
                new eManager.Domain.Department() { NameDep = "Developer" },
                new eManager.Domain.Department() { NameDep = "Human Resource" },
                new eManager.Domain.Department() { NameDep = "Marketing" },
                new eManager.Domain.Department() { NameDep = "Sales" }

                );
        }
    }
}

In Controllers > HomeController.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using eManager.Domain;
using Data;
using PluralSight_MVC_Ejercicio.Infrastructure;

namespace PluralSight_MVC_Ejercicio.Controllers
{
    public class HomeController : Controller
    {

        //seleccionar el entityframework database = agregar como new en (interfaces) con el nombre del DB que se usara con las 2 
        //tablas de employee y deparment
        //private DataDepartamentSource data = new DepartmentDataBase();

        private Data.DataDepartamentSource db = new DepartmentDataBase();

        public HomeController(DepartmentDataBase data)
        {
            db = data;
        }


        [HttpGet]
        public ActionResult Index()
        {
            var alldepartment = db.Departments;

            return View(alldepartment);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

In Views > Home > Index.cshtml:

@model IEnumerable<eManager.Domain.Department>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.NameDep)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.NameDep)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.IdDep }) |
            @Html.ActionLink("Details", "Details", new { id=item.IdDep }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.IdDep })
        </td>
    </tr>
}

</table>

Well, I used the package manager console to create the database and I enabled the migrations, but I still can't show the information in the view... so, can anybody help me?

2
  • I might be wrong but I think MVC expects your Controller to have a parameterless constructor, which it doesn't. Commented May 11, 2016 at 23:08
  • For some reason I think its your HomeController that needs a constructor without a parameter. If it is then you probably aren't using it yet. Commented May 11, 2016 at 23:09

2 Answers 2

2

(As it was mentioned in the comments.) AFAIK, MVC creates the controller for you, so it needs a default constructor. Also, a default constructor is not automatically generated for you if you add a constructor of your own.

Here is an overview of the MVC 5 pipeleine (I assume that's the version you're using): http://www.asp.net/mvc/overview/getting-started/lifecycle-of-an-aspnet-mvc-5-application (Try the PDF version if you can't make out the diagram.)

You can see it shows a controller factory creating the controller for you.

Anyway, I think if you just add a default constructor, it would solve that error.

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

Your HomeController expects an argument of type DepartmentDataBase when it is constructed, but in this case, ASP.NET MVC does not know how to initialise the controller with that argument in the absence of a custom controller factory. So, ASP.NET uses the default controller factory which only builds controllers with default (or parameter-less) constructors, which you do not have in your code.

You are doing the right thing by trying to inject your dependency in the constructor; you just need to add a container that comes with a custom controller factory. Personally, I use Unity Container, but you have other choices like Autofac and Castle Windsor.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.