0
I need a custom Viewbag, which brings the name of the teacher and the id, referring to the registered school, for this I did:
ViewBag.ProfessorID = new SelectList(from u in db.Pessoas.OfType<Professor>() join v in db.EscolaProfessores on u.PessoaID equals v.ProfessorID where v.EscolaID== escola.EscolaID select u).ToList();
Codes: Seek active user:
Escola escola = Repositories.Funcoes.GetUsuario();
Classes:
public class Pessoa
{
[Key]
public int PessoaID { get; set; }
[DisplayName("Nome")]
[Required(ErrorMessage = "Preencha o nome")]
[StringLength(255, MinimumLength = 3, ErrorMessage = "O nome deve ter de 3 a 255 caracteres")]
public string Nome { get; set; }
{...}
//relacionamentos
public int CidadeID { get; set; }
public virtual Cidade Cidade { get; set; }
}
public class Professor : Pessoa
{
//Relacionamentos
public virtual ICollection<EscolaProfessor> Escolas { get; set; }
public virtual ICollection<ProfessorAluno> Alunos { get; set; }
}
public class Escola
{
[Key]
public int EscolaID { get; set; }
[DisplayName("Razão Social")]
[Required(ErrorMessage = "Preencha a Razão Social")]
[StringLength(255, MinimumLength = 3, ErrorMessage = "A razão social deve ter de 3 a 255 caracteres")]
public string RazaoSocial { get; set; }
{...}
//relacionamentos
public int CidadeID { get; set; }
public virtual Cidade Cidade { get; set; }
public virtual ICollection<EscolaProfessor> Professores { get; set; }//Vários professores
public virtual ICollection<EscolaAluno> Alunos { get; set; }//Vários alunos
}
public class EscolaProfessor
{
[Key]
public int EscolaProfessorID { get; set; }
//Relaiconamentos
[ForeignKey("Professor")]
public int ProfessorID { get; set; }
public virtual Professor Professor { get; set; }
[ForeignKey("Escola")]
public int EscolaID { get; set; }
public virtual Escola Escola { get; set; }
}
and here the code in the View:
<div class="form-group">
@Html.LabelFor(model => model.ProfessorID, "Professor", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ProfessorID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ProfessorID, "", new { @class = "text-danger" })
</div>
</div>
And then in the view, the Dropdownlist displays the results as follows:
MinhaAplicacao.Models.Professor
And not their name.
Edit your question and place the part where you display the
ViewBag.ProfessorID
, it seems that the problem is just when you "catch" theViewBag
in the view– Marllon Nasser
@Marllonnasser you say to put the view code? if it is just edit
– Fabio Souza
yes.. the problem is that you are storing the list inside the viewbag and are not using viewBag in the view itself...
– Marllon Nasser
And what exactly do I have to do ?
– Fabio Souza
@Fabiosouza You want the drop down show
nome - id
? Something like:Jéferson - 550
. Or is it something else?– Jéf Bueno
Show only the name, but behind each user has their 'hidden' id as it usually is
– Fabio Souza
And what’s the problem?
– Jéf Bueno