I have a Academic entity that has a Teacher , I want the Academic Detail Professor who belongs to this Academician .
The code I'm already using is this:
Teacher 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; }
}
Academic Model:
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; }
}
Academic details view:
<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);
}
}