Select two attributes from two different tables in a view

Asked

Viewed 195 times

1

I have the model (superclass) Pessoa and the model Aluno

public partial class Pessoa
{
    public Pessoa()
    {
        this.Escola = new HashSet<Escola>();
    }
    [Key]
    public int PessoaID { get; set; }           
    public String Nome { get; set; }
    public String Morada { get; set; }          
    public virtual ICollection<Escola> Escola { get; set; }
}

public partial class Aluno
{
    public Aluno()

    [Key, ForeignKey("Pessoa")]
    public int AlunoID { get; set; }    
    public String NomeEscola { get; set; }
    public String AnoEscolar { get; set; }
    public String TurmaEscolar { get; set; }    
    public virtual Pessoa Pessoa { get; set; }
}

I intend to make a selectlist of the Name attribute in a view where Alunoid is equal to Pessoaid I tried with the following code on the Student’s controller but without success

public ActionResult Edit(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Aluno aluno = db.aluno.Find(id);
    if (aluno == null)
    {
        return HttpNotFound();
    }
    ViewBag.AlunoID = new SelectList(db.Pessoa, "PessoaID", "Nome", aluno.AlunoID);
    return View(aluno);
}
  • Let me get this straight. You want to take your contexto, all objects that have the property PessoaID equal to aluno.AlunoID?

  • Yes, it was intended to obtain the names of the Person table that corresponded to the ID of the Student table, in which these names would be sent to a Listbox. I’ve already created a Viewmodel that I think is necessary...

1 answer

1

Your modeling is incorrect. A Aluno is a Pessoa, therefore Aluno must be a derivation of Pessoa:

public partial class Pessoa
{
    // Isto está incorreto, então comentei. 
    // Quem inicializa propriedades de navegação é o Entity Framework.
    /* public Pessoa()
    {
        this.Escola = new HashSet<Escola>();
    } */

    [Key]
    public int PessoaID { get; set; }           
    public String Nome { get; set; }
    public String Morada { get; set; }          
    public virtual ICollection<Escola> Escola { get; set; }
}

public partial class Aluno : Pessoa
{
    // Este construtor está sem utilidade, então também retirei.
    // public Aluno()
    public String NomeEscola { get; set; }
    public String AnoEscolar { get; set; }
    public String TurmaEscolar { get; set; }    

    // Esta propriedade de navegação não é necessária.
    // public virtual Pessoa Pessoa { get; set; }
}

In context, it’s interesting to have both DbSet, so much of Pessoa how much of Aluno, mapped:

    public DbSet<Aluno> Alunos { get; set; }
    public DbSet<Pessoa> Pessoas { get; set; }

If you want to select any type of person, use contexto.Pessoas. If you want to specifically select students, use contexto.Alunos.

Your Controller will look like this:

public ActionResult Edit(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Aluno aluno = db.aluno.Find(id);
    if (aluno == null)
    {
        return HttpNotFound();
    }
    ViewBag.AlunoID = new SelectList(db.Alunos, "PessoaID", "Nome", aluno.AlunoID);
    return View(aluno);
}
  • I cannot update database derived from the Student Person inheritance

  • Then possibly you will have to generate your Migrations again. This answer radically changes your modeling.

Browser other questions tagged

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