Custom Viewbag, how to do?

I need a 'custom' Viewbag, which brings the name of the teacher and the id, referring to the school logged in, 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: Search 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 ai in the view, the Dropdownlist displays the results as follows:

MinhaAplicacao.Models.Professor

And not their name.

Author: Fabio Souza, 2017-01-31

2 answers

At the time of the creation of the SelectList , I missed putting the two fields that are important elements in the assembly of the select of the html, and I made an adjustment in its ViewBag to ViewBag.Professores, because, already has a field with that name, example :

var result = 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;

ViewBag.Professores = new SelectList(result, "ProfessorID","Nome");

In @Html.DropDownList, also put the same name as the ViewBag Professores, example:

<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("Professores", null, 
           htmlAttributes: new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.ProfessorID, "", 
           new { @class = "text-danger" })
    </div>
</div>

Reading: a simple technique to use DropDownList in ASP.NET MVC

references:

 2
Author: novic, 2017-01-31 19:18:33

The problem is that you don't put your list in your dropdown.

Adjusts your old list to return only one Collection.

var listaProfessores = 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;

ViewBag.ListaProfessores = listaProfessores;

And in view use that list in your dropdown.

<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", new SelectList((IEnumerable<Professor>)ViewBag.ListaProfessores, "PessoaID", "Nome"), new { @class = "form-control" })
           @Html.ValidationMessageFor(model => model.ProfessorID, "", new { @class = "text-danger" })
       </div>
</div>

But I recommend that you create a ViewModel with a property IEnumerable<Professor> and pass that information to the view.

 1
Author: Marllon Nasser, 2017-01-31 19:16:46