Custom Viewbag, how to do it?

0

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

MinhaAplicacao.Models.Professor

And not their names.

    
asked by anonymous 31.01.2017 / 19:54

2 answers

2

When creating the SelectList , I failed to put the two fields that are important elements in the assembly of select of html , and I made an adjustment in its ViewBag to ViewBag.Professores , because it 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 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 for using DropDownList in ASP.NET MVC

References

  • SelectList Class
  • ASP.Net MVC - ViewData, ViewBag, and TempData
  • ASP.Net MVC - ViewData, ViewBag and TempData
  • a>
31.01.2017 / 20:05
1

The problem is that you do not put your List in your dropdown.

Adjust your old list to return only a 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 the 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 IEnumerable<Professor> view property and pass that information to the view.

    
31.01.2017 / 20:10