首页 > 解决方案 > InvalidOperationException:传递到 ViewDataDictionary 的模型项的类型为“WebApplication1.Models.Socios”

问题描述

我在 aspnet.core 中有一个带有登录和注销的程序。当我在登录页面上以合作伙伴身份登录时,我有一个“教师”布局,它应该向我显示现有教师的列表。此方法在布局中获得为

<li class="nav-item">
<a class="nav-link text-white" asp-controller="Professores" asp-action="ListarProfessores">Professores</a>

</li>

这个方法是在教师驱动程序中创建的,我添加了一个包含班级教师的列表视图。在教师答案列表中,我有以下代码:

       @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>

有人可以帮我解决这个问题吗?

InvalidOperationException:传递到 ViewDataDictionary 的模型项的类型为“WebApplication1.Models.Socios”,但此 ViewDataDictionary 实例需要“System.Collections.Generic.IEnumerable`1[WebApplication1.Models.Professores]”类型的模型项

在教师控制器中,我有这个方法:

 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();
    }

所以我有两个班:教师班和社会班。当我以合作伙伴身份登录时,我想查看表中的教师列表并选择一位作为私人教练。在社会阶层中,我有:

 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; }
    }
}

在私人教练课程中,我有: 私人教练由成员通过现有教师选择

 [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; }
    }
}

标签: c#htmlasp.net-core

解决方案


发生错误是因为您的控制器代码return View(y)返回一个Socio模型,而您的视图接收IEnumerable<WebApplication1.Models.Professores>为模型。它们应该是相同的类型。

我有单独的班级,我有一个社会班和一个教师班。

Socio然后我们需要知道和之间的关系Professores(你最好展示所有必要的模型代码及其关系)和返回列表Professores

例如,如果Socio

public class Socio
{
   //other properties
   public List<Professores> Professores {get;set;}
}

然后使用return View(y.Professores);


推荐阅读