Show fields of an entity in a different View

Asked

Viewed 58 times

3

I have an entity Academic who owns a Professor, I want in the View details of Academic the teacher responsible for it, i.e. Professor that belongs to that Academic.

The code I’m already using is this:

Teacher’s model:

 public class Professor : RepositorioBase<Professor>
 {
    [Key]
    public int IdProfessor { get; set; }

    public List<Academico> Academicos { get; set; }

    public Usuario Usuario { get; set; }

    [Display(Name = "Matrícula")]
    public int Matricula { get; set; }

    public string Nome { get; set; }

    [Display(Name = "E-mail")]
    [EmailAddress(ErrorMessage = "E-mail em formato inválido.")]
    public string Email { get; set; }

    [Display(Name = "Data de cadastro")]
    public DateTime DataCadastro { get; set; }

    [Display(Name = "Atívo")]
    public bool Ativo { get; set; }

    [Display(Name = "Estagiário")]
    public int IdAcademico { get; set; }
}

Model of the academic:

 public class Academico : RepositorioBase<Academico>
 {
    [Key]
    public int IdAcademico { get; set; }

    [Display(Name = "Matrícula")]
    public int Matricula { get; set; }

    public string Nome { get; set; }

    [Display(Name = "E-mail")]
    [EmailAddress(ErrorMessage = "E-mail em formato inválido.")]
    public string Email { get; set; }

    [Display(Name = "Data de cadastro")]
    public DateTime DataCadastro { get; set; }

    [Display(Name = "Atívo")]
    public bool Ativo { get; set; }

    [Display(Name = "Professor orientador")]
    public int IdProfessor { get; set; }

    [Display(Name = "Pacientes")]
    public List<Paciente> Pacientes { get; set; }

    public virtual Professor Professor { get; set; }
}

View details of the academic:

<div id="main" class="container-fluid">
<h3 class="page-header"> Detalhes de @Html.DisplayFor(model => model.Nome)</h3>
<div class="row">

    <div class="col-md-4">
        <p><strong>@Html.DisplayNameFor(model => model.Matricula)</strong></p>
        <p>@Html.DisplayFor(model => model.Matricula)</p>
    </div>

    <div class="col-md-4">
        <p><strong>@Html.DisplayNameFor(model => model.Professor.Nome)</strong></p>
        <p>@Html.DisplayFor(model => model.Professor.Nome)</p>
    </div>
    <div class="col-md-4">
        <p><strong>@Html.DisplayNameFor(model => model.Nome)</strong></p>
        <p>@Html.DisplayFor(model => model.Nome)</p>
    </div>
    <div class="col-md-4">
        <p><strong>@Html.DisplayNameFor(model => model.Email)</strong></p>
        <p>@Html.DisplayFor(model => model.Email)</p>
    </div>
    <div class="col-md-4">
        <p><strong>@Html.DisplayNameFor(model => model.DataCadastro)</strong></p>
        <p>@Html.DisplayFor(model => model.DataCadastro)</p>
    </div>

    <div class="col-md-4">
        <p><strong>@Html.DisplayNameFor(model => model.Ativo)</strong></p>
        <p>@Html.DisplayFor(model => model.Ativo)</p>
    </div>
</div>

Academic controller:

 public class AcademicoController : Controller
{
    private PepContext db = new PepContext();

    // GET: Academico
    public ActionResult Index()
    {
        var academicos = db.Academicos.Include(a => a.Professor);
        return View(academicos.ToList());
    }

    // GET: Academico/Details/5
    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Academico academico = db.Academicos.Find(id);
        if (academico == null)
        {
            return HttpNotFound();
        }
        return View(academico);
    }

    // GET: Academico/Create
    public ActionResult Create()
    {
        ViewBag.IdProfessor = new SelectList(db.Professores, "IdProfessor", "Nome");
        return View();
    }

    // POST: Academico/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "IdAcademico,Matricula,Nome,Email,DataCadastro,Ativo,IdProfessor,IdPaciente")] Academico academico)
    {
        if (ModelState.IsValid)
        {
            db.Academicos.Add(academico);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.IdProfessor = new SelectList(db.Professores, "IdProfessor", "Nome", academico.IdProfessor);
        return View(academico);
    }

    // GET: Academico/Edit/5
    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Academico academico = db.Academicos.Find(id);
        if (academico == null)
        {
            return HttpNotFound();
        }
        ViewBag.IdProfessor = new SelectList(db.Professores, "IdProfessor", "Nome", academico.IdProfessor);
        return View(academico);
    }

    // POST: Academico/Edit/5
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "IdAcademico,Matricula,Nome,Email,DataCadastro,Ativo,IdProfessor,IdPaciente")] Academico academico)
    {
        if (ModelState.IsValid)
        {
            db.Entry(academico).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.IdProfessor = new SelectList(db.Professores, "IdProfessor", "Nome", academico.IdProfessor);
        return View(academico);
    }

    // GET: Academico/Delete/5
    public ActionResult Delete(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Academico academico = db.Academicos.Find(id);
        if (academico == null)
        {
            return HttpNotFound();
        }
        return View(academico);
    }

    // POST: Academico/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        Academico academico = db.Academicos.Find(id);
        db.Academicos.Remove(academico);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }
}
  • Why not create a navigation property?

  • Where can I find examples ?

  • You use Entity Framework?

  • I’m using yes

  • See my answer.

1 answer

2


As you are using Entity Framework, this is quite simple to do.

You can use the navigation property Professor that exists in Academico.

To show, you can do the following on view:

<div class="col-md-4">
    <p>@Model.Professor.Nome</p>
</div>
  • In case I want to present the list of Academics that belongs to the teacher, how can it be done ?

Browser other questions tagged

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