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?
Controllerto have a parameterless constructor, which it doesn't.