C# InvalidOperationException:传递到ViewDataDictionary的模型项的类型为';WebApplication1.Models.Socios';
我在aspnet.core中有一个带有登录和注销的程序。当我在登录页面上以合作伙伴身份登录时,我的“教师”布局应该会显示现有教师的列表。 此方法在布局中获得,如下所示:C# InvalidOperationException:传递到ViewDataDictionary的模型项的类型为';WebApplication1.Models.Socios';,c#,html,asp.net-core,C#,Html,Asp.net Core,我在aspnet.core中有一个带有登录和注销的程序。当我在登录页面上以合作伙伴身份登录时,我的“教师”布局应该会显示现有教师的列表。 此方法在布局中获得,如下所示: <li class="nav-item"> <a class="nav-link text-white" asp-controller="Professores" asp-action="ListarProfessores">Professores</a> </li> 所以我有
<li class="nav-item">
<a class="nav-link text-white" asp-controller="Professores" asp-action="ListarProfessores">Professores</a>
</li>
所以我有两个班:教师班和社会班。当我以合作伙伴身份登录时,我想在表中查看教师列表,并选择一位作为私人教练。
在社会阶层中,我有:
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; }
}
}
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; }
}
}
由于控制器代码返回视图(y)返回一个
Social
模型,而视图接收到IEnumerable
作为模型,因此发生此错误。它们应该是相同的类型
我有单独的班级,我有社会课和教师课
然后我们需要知道社会
和教授
之间的关系(您最好显示所有必要的模型代码及其关系),并返回教授
列表
例如,如果social
具有
public class Socio
{
//other properties
public List<Professores> Professores {get;set;}
}
公共类
{
//其他属性
公开名单教授{get;set;}
}
然后使用
返回视图(y.教授)代码>你能分享控制者行动的代码吗?@ChetanRanpariya是的,当然,我编辑了这篇文章,并将它放在控制者方法中。你正在将类Socios
传递给视图,而不是教授的列表。也许Socios
有一个professors
成员列表,如果是这样,你只需要将return View(y)
替换为return View(y.professors)
@George您好,我有单独的班级,我有一个社会班级和一个教师班级。我想在合作伙伴登录时在数据库中显示教师列表。您好,我在课堂上没有列表,我用课堂上的内容更新了出版物。@rsd_17正如我所说,操作返回类型需要与接收视图类型相匹配。那么我该怎么做?
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; }
}
}
[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; }
}
}
public class Socio
{
//other properties
public List<Professores> Professores {get;set;}
}