InvalidOperationException:传递到ViewDataDictionary的模型项的类型为'System.Int32' [英] InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Int32'

查看:90
本文介绍了InvalidOperationException:传递到ViewDataDictionary的模型项的类型为'System.Int32'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法显示特定表元素的数据.该程序允许登录和注销.所以我有私人教练课

I'm having trouble presenting the data for a specific table element. The program allows login and logout. so I have the Personal Trainer class

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

我有Socios课

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

因此,我希望以会员身份登录时可以通过列表查看注册教师的资料,并进行详细选择并查看.问题在于,该视图出现了,但只显示了字段名称数据不出现.我想通过我的个人资料(以合作伙伴身份登录)查看我的私人教练的个人资料.在Socios控制器中,我具有此功能是为了向我显示我的私人教练的个人资料.通过这种方法,我创建了一个视图详细信息.

So I want that when I log in as a Member I can see the profile of registered teachers through a list and select in detail and see. The problem is that the view appears, but appears only with the name of the fields The data does not appear. And I want to see through my profile, logged in as a partner, the profile of my personal Trainer. In the Socios controller I have this function in order to show me the profile of my personal Trainer.through this method I created a view details.

 public IActionResult VerPT(PersonalTrainer personalTrainer, Professores professores, int? id)
        {
            int x = Convert.ToInt32(HttpContext.Session.GetInt32("UserId"));


            foreach (var item in _context.Socios)
            {


                if (item.NomeUtilizador == User.Identity.Name)
                {
                    personalTrainer.Idsocio = item.Idsocio;
                    personalTrainer.Idprofessor = professores.Idprofessor;
                    id = personalTrainer.Idprofessor;
                }
            }
            return View(personalTrainer);
        }

在控制老师中,我具有此功能,以便能够查看老师的个人资料,通过这种方法,我创建了视图详细信息.

In the controller teacher I have this function in order to be able to see the profile of the teacher, through this method I created a view details.

public async Task<IActionResult> VerProfessor(int? id)
        {

            if (id == null)
            {
                return NotFound();
            }
            var professores = await _context.Professores
                .FirstOrDefaultAsync(m => m.Idprofessor == id);
            if (professores == null)
            {
                return NotFound();
            }

            return View();



        }

我在verPT视图中

@model WebApplication1.Models.PersonalTrainer

@{
    ViewData["Title"] = "VerPT";
}

    <style>
        body {
            padding-top: 0px;
            background-color: darkgray;
            background-image: url();
            background-image: url();
            background-repeat: no-repeat;
        }
    </style>

<div>
    <h4>Personal Trainer</h4>
    <hr />
    <dl class="row">
        <dt class = "col-sm-2">
            Data de inicio :
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.DataInicio)
        </dd>

        <dt class = "col-sm-2">
            Email 
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.IdprofessorNavigation.Email)
        </dd>

    </dl>
</div>
<div>

    <a asp-action="PerfilSocio">Voltar</a>
</div>

我在verprofesssor视图中

In the verprofesssor view I have

@model WebApplication1.Models.Professores
@{
    ViewData["Title"] = "VerProfessor";
}

<style>

    body {
        padding-top: 0px;
        background-color: gray;
        background-image: url();
        background-image: url();
        background-repeat: no-repeat;
    }
</style>
<br />
<br />

<div style="color:white">


    <dl class="row">
        @*<dt class="col-sm-2">
                @Html.DisplayNameFor(model => model.Fotografia)
            </dt>*@
        <dd class="col-sm-10">
            <img src="~/Fotos/ + model.Fotografia " />

        </dd>
        <br />

        <p>Contactos </p>

        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Nome)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Nome)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Email)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Email)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Telefone)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Telefone)
        </dd>

        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Sexo)
        </dt>
        <dd class="col-sm-10">
            @if (@Html.DisplayNameFor(model => model.Sexo) == "true")
            {
                <p>Feminino</p>
            }
            else
            {
                <p>Masculino</p>
            }
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Especialidade)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Especialidade)
        </dd>

    </dl>
</div>

<div>

    <a asp-action="ListarProfessores">Voltar < </a>
</div>

<br />
<br />
<br />
<br />
<br />
<br />

推荐答案

错误很明显:您需要将 Model 类型发送到 View ,但是您正在发送 int (在您的情况下为 id ).由于您需要在视图"中显示培训师资料,因此需要将模型类型更改为:

The error is obvious: You need to send a Model type to your View but you are sending an int which is id in your case. Since you need to show the trainer profile in your View, you would need to change the model type to:

WebApplication1.Models.PersonalTrainer 

在您的 View

return View(personalTrainer) 

在您的 Controller

这篇关于InvalidOperationException:传递到ViewDataDictionary的模型项的类型为'System.Int32'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆