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.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());
}
}
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 yourController
to see how his return is.– Randrade
@Randrade mine Controller that’s the one
– Alerf Morais