Customized Viewbag, how to do?

Asked

Viewed 161 times

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" the ViewBag in the view

  • @Marllonnasser you say to put the view code? if it is just edit

  • yes.. the problem is that you are storing the list inside the viewbag and are not using viewBag in the view itself...

  • And what exactly do I have to do ?

  • @Fabiosouza You want the drop down show nome - id? Something like: Jéferson - 550. Or is it something else?

  • Show only the name, but behind each user has their 'hidden' id as it usually is

  • And what’s the problem?

Show 2 more comments

2 answers

2


At the time of the creation of the Selectlist, failed to place the two fields that are important elements in the assembly of the select of html, and made an adjustment to your ViewBag for 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 the @Html.DropDownList, also put the same name of 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:

  • 1

    Thanks Virgilio, it worked!!

1

The problem is that you don’t put your List in your dropdown.

Adjust 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 the view use such 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 this information to the view.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.