The template item entered in the dictionary is of type 'System.Collections.Generic.List'

1

I'm developing a project where I have the Patient class inheriting from the User class and I'm using the Entity Framework to map the database tables by type (TPT), when I try to access the browser from the Class View Patient I get the following error :

  

The template item entered in the dictionary is from   type 'System.Collections.Generic.List 1[Projeto.Models.Usuario]', mas esse dicionário requer um item do tipo 'System.Collections.Generic.IEnumerable 1 [Project.Models.Patient]'.

User class:

public  class Usuario : RepositorioBase<Usuario>, IUsuario

{ 
    public int Id { get; set; }

    public int Matricula { get; set; }

    public string Nome { get; set; }

    public int Email { get; set; }

    public DateTime DataCadastro { get; set; }

    public bool Ativio { get; set; }

}

Patient Class:

 public class Paciente : Usuario
    {

        public string Apelido { get; set; }

        public int Idade { get; set; }
}

Context:

public class Context : DbContext
    {
        public PepContext()
            : base("Context")
        {


        }

        public DbSet<Usuario> Usuario { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            modelBuilder.Configurations.Add(new PacienteConfiguration());


        }
}

PatientConfiguration:

public class PacienteConfiguration: EntityTypeConfiguration<Paciente>
    {
        public PacienteConfiguration()
        {
            ToTable("Pacientes");
        }
    }

Patient Class View Model:

@model IEnumerable<Projeto.Models.Paciente>

Controller:

 public class PacienteController : Controller
    {
        readonly Paciente _paciente;

        public PacienteController(Paciente paciente)
        {
            _paciente = paciente;
        }

        // GET: Paciente
        public ActionResult Index()
        {
            var paciente = _paciente.GetAll();
            return  View(paciente);
        }

        // GET: Paciente/Details/5
        public ActionResult Details(int id)
        {
            var paciente = _paciente.GetById(id);
            return View();
        }

        // GET: Paciente/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Paciente/Create
        [HttpPost]
        public ActionResult Create(Paciente paciente)
        {
            if (ModelState.IsValid)
            {
                _paciente.Add(paciente);

                return RedirectToAction("Index");
            }

            return View(paciente);
        }

        // GET: Paciente/Edit/5
        public ActionResult Edit(int id)
        {
            var paciente = _paciente.GetById(id);
            return View();
        }

        // POST: Paciente/Edit/5
        [HttpPost]
        public ActionResult Edit(Paciente paciente)
        {
            if (ModelState.IsValid)
            {
                _paciente.Update(paciente);

                return RedirectToAction("Index");
            }

            return View(paciente);
        }

        // GET: Paciente/Delete/5
        public ActionResult Delete(int id)
        {
            var paciente = _paciente.GetById(id);
            return View(paciente);
        }

        // POST: Paciente/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult Delete(int id, FormCollection collection)
        {
            var paciente = _paciente.GetById(id);
            _paciente.Remove(paciente);

            return RedirectToAction("Index");
        }
    }
    
asked by anonymous 03.10.2017 / 06:11

1 answer

0

The problem occurs because you can not implicitly convert a List to type IEnumerable. You must change your Patient.GetAll method (converting the return of type List to IEnumerable) or the type of model (IEnumerable to List) in your view.

See below:

Change in the GetAll method:


    var pacientes = pacienteDb.ListarPacientes()
    return pacientes.AsEnumerable();

Or change the view model to the type List:


    @model List<Projeto.Models.Paciente>

    
03.10.2017 / 19:34