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

Asked

Viewed 2,364 times

1

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

The template item inserted in the dictionary is from type'System.Collections.Generic.List1[Projeto.Models.Usuario]', mas esse dicionário requer um item do tipo 'System.Collections.Generic.IEnumerable1[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());


        }
}

Patienteconfiguration:

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

Model of the Patient class View:

@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");
        }
    }
  • Your view expects a Enumerable() of patient but you are passing a list of User. Post your Controller to see how his return is.

  • @Randrade mine Controller that’s the one

1 answer

0


The problem is because it is not possible to convert a List to the Ienumerable type implicitly. You should change your Patient.Getall method (converting the List to Ienumerable type return) or the model type (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 List type:


    @model List<Projeto.Models.Paciente>

  • I changed the Getall method but gave the same error, changed the model to list gave the following error: 'List<Patient>' does not contain a Definition for 'Matricula' and no Extension method 'Matricula' Accepting a first argument of type 'List<Patient>' could be found (are you Missing a using Directive or an Assembly Reference?

  • In that case I recommend you to create a Viewmodel, Because the Binding of fields is done upon the class placed as argument, and it does not understand inheritance. That’s why you can’t find the Matricula property.

  • I’ll follow your lead, thank you

  • I need to map a domain model to a View Model, that’s it ?

  • That. Take a look at Automapper http://www.eduardopires.net.br/2013/08/asp-net-mvc-utilizando-automapper-com-view-model-pattern/ and https://github.com/AutoMapper/AutoMapper

  • Okay, one more question rsrs, the class Patienteviewmodel must inherit from the class User ?

  • No. It should contain all Patient and User fields.

  • Okay, thanks for now

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.