Reputation: 75
I have a program in aspnet.core with login and logout. When I log in as a partner on the login page I have the "Teachers" layout that should show me the list of existing teachers. This method is obtained in the layout as
<li class="nav-item">
<a class="nav-link text-white" asp-controller="Professores" asp-action="ListarProfessores">Professores</a>
</li>
This method is created in the Teachers driver, I add a list view with the teachers in the class. In the Teachers answer list, I have this code:
@model IEnumerable<WebApplication1.Models.Professores>
@{
ViewData["Title"] = "ListarProfessores";
}
<style>
body {
padding-top: 0px;
background-color: gray;
background-image: url();
background-image: url();
background-repeat: no-repeat;
}
</style>
<h1>Professores Registados</h1>
<table class="table" style="color:white">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Nome)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Nome)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
<a asp-controller="Socios" asp-action="EscolherPT" asp-route-id="@item.Idprofessor">Escolher PT</a>
</td>
</tr>
}
</tbody>
</table>
Can someone help me solve this please?
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'WebApplication1.Models.Socios', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable`1[WebApplication1.Models.Professores]'
In the Teachers controller I have this method:
public IActionResult ListarProfessores()
{
int x = Convert.ToInt32(HttpContext.Session.GetInt32("UserId"));
foreach (var item in _context.Socios)
{
if (item.Idsocio == x)
{
var y = item;
return View(y);
}
}
return View();
}
So I have two classes: the teachers class and the social class. When I am logged in as a partner I wanted to see a list of the teachers in the table and select one to be a personal trainer. In the social class I have:
public partial class Socios
{
public Socios()
{
Gerir = new HashSet<Gerir>();
Mensagem = new HashSet<Mensagem>();
Participa = new HashSet<Participa>();
PersonalTrainer = new HashSet<PersonalTrainer>();
Peso = new HashSet<Peso>();
PlanosExercicios = new HashSet<PlanosExercicios>();
}
[Key]
[Column("IDSocio")]
public int Idsocio { get; set; }
[Required]
[Column("email")]
[StringLength(100)]
public string Email { get; set; }
[Required]
[Column("telefone")]
[StringLength(20)]
public string Telefone { get; set; }
[Required]
[Column("fotografia")]
[StringLength(40)]
public string Fotografia { get; set; }
[Column("sexo")]
public bool Sexo { get; set; } // true- Feminino
// false" - Masculino
[Column("altura")]
public double Altura { get; set; }
[Required]
[Column("nome_utilizador")]
[StringLength(50)]
public string NomeUtilizador { get; set; }
[Column("peso_inicial")]
public double PesoInicial { get; set; }
[Required]
[Column("_password")]
[StringLength(20)]
public string Password { get; set; }
[Column("estado")]
public int Estado { get; set; } // 1 ativo, 0 suspenso
//[Column("mensalidade")]
//public bool Mensalidade { get; set; } //0-nao pago 1-pago
[InverseProperty("IdsocioNavigation")]
public virtual ICollection<Gerir> Gerir { get; set; }
[InverseProperty("IdsocioNavigation")]
public virtual ICollection<Mensagem> Mensagem { get; set; }
[InverseProperty("IdsocioNavigation")]
public virtual ICollection<Participa> Participa { get; set; }
[InverseProperty("IdsocioNavigation")]
public virtual ICollection<PersonalTrainer> PersonalTrainer { get; set; }
[InverseProperty("IdsocioNavigation")]
public virtual ICollection<Peso> Peso { get; set; }
[InverseProperty("IdsocioNavigation")]
public virtual ICollection<PlanosExercicios> PlanosExercicios { get; set; }
}
}
In the teachers class I have:
public partial class Professores
{
public Professores()
{
MapaAulasGrupo = new HashSet<MapaAulasGrupo>();
Mensagem = new HashSet<Mensagem>();
PersonalTrainer = new HashSet<PersonalTrainer>();
Peso = new HashSet<Peso>();
PlanosExercicios = new HashSet<PlanosExercicios>();
}
[Key]
[Column("IDProfessor")]
public int Idprofessor { get; set; }
[Required]
[Column("nome")]
[StringLength(50)]
public string Nome { get; set; }
[Required]
[Column("email")]
[StringLength(100)]
public string Email { get; set; }
[Column("telefone")]
public int Telefone { get; set; }
[Required]
[Column("fotografia")]
public string Fotografia { get; set; }
[Column("sexo")]
public bool Sexo { get; set; }
[Required]
[Column("especialidade")]
[StringLength(50)]
public string Especialidade { get; set; }
[Column("estado")]
public int Estado { get; set; }
[Required]
[Column("_password")]
[StringLength(20)]
public string Password { get; set; }
[InverseProperty("IdprofessorNavigation")]
public virtual ICollection<MapaAulasGrupo> MapaAulasGrupo { get; set; }
[InverseProperty("IdprofessorNavigation")]
public virtual ICollection<Mensagem> Mensagem { get; set; }
[InverseProperty("IdprofessorNavigation")]
public virtual ICollection<PersonalTrainer> PersonalTrainer { get; set; }
[InverseProperty("IdprofessorNavigation")]
public virtual ICollection<Peso> Peso { get; set; }
[InverseProperty("IdprofessorNavigation")]
public virtual ICollection<PlanosExercicios> PlanosExercicios { get; set; }
}
}
In the Personal Trainer class I have: Personal trainers are chosen by members through existing teachers
[Table("Personal_trainer")]
public partial class PersonalTrainer
{
[Key]
[Column("IDProfessor")]
public int Idprofessor { get; set; }
[Key]
[Column("IDSocio")]
public int Idsocio { get; set; }
[Key]
[Column("Data_Pedido", TypeName = "date")]
public DateTime DataPedido { get; set; }
[Column("data_Inicio", TypeName = "date")]
public DateTime? DataInicio { get; set; }
[Column("data_fim", TypeName = "date")]
public DateTime? DataFim { get; set; }
[ForeignKey(nameof(Idprofessor))]
[InverseProperty(nameof(Professores.PersonalTrainer))]
public virtual Professores IdprofessorNavigation { get; set; }
[ForeignKey(nameof(Idsocio))]
[InverseProperty(nameof(Socios.PersonalTrainer))]
public virtual Socios IdsocioNavigation { get; set; }
}
}
Upvotes: 0
Views: 565
Reputation: 20116
The error occurs since that your controller code return View(y)
returns a Socio
model while your view receives IEnumerable<WebApplication1.Models.Professores>
as model.They are expected to be the same type.
I have separate classes, I have a socio class and a teacher class.
Then we need to know the relationships between Socio
and Professores
(you'd better show all necessary models code and their relationships) and return list of Professores
.
For example, if Socio
has
public class Socio
{
//other properties
public List<Professores> Professores {get;set;}
}
Then use return View(y.Professores);
Upvotes: 1