I had a problem making relationships in my application. My project is from a school, and in it I have to have the occurrences. But at the time of relating the Student to the Occurrence it happened to me that, in the view create of the occurrences, I see a dropdownlist with the registered students, to make the relationship, that is, to map the occurrence in the student, I say, what the student made ready What I really wanted was that when I uploaded the details of the student, I had a collision (accordion type) where the user could put the occurrence for that particular student, taking his Id, ie, passing the id of the student's view details and the same student's ID as the correct mapping of the occurrence. That is, it would record the occurrence for that student, and not dropdaownlist with the students.
I'll put the student's view view here to see what can be done:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Ocorrencia</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Tipo, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Tipo)
@Html.ValidationMessageFor(model => model.Tipo)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Causa, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Causa)
@Html.ValidationMessageFor(model => model.Causa)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Observacao, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Observacao)
@Html.ValidationMessageFor(model => model.Observacao)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AlunoId, "AlunoId", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("AlunoId", String.Empty)
@Html.ValidationMessageFor(model => model.AlunoId)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Salvar" class="btn btn-default" />
</div>
</div>
</div>
}
Here is the part of the controller that populates the dropdownlist:
public ActionResult Index()
{
var ocorrencias = db.Ocorrencias.Include(o => o.Aluno);
return View(ocorrencias.ToList());
}
// GET: /Ocorrencias/Detalhes/5
public ActionResult Detalhes(long? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Ocorrencia ocorrencia = db.Ocorrencias.Find(id);
if (ocorrencia == null)
{
return HttpNotFound();
}
return View(ocorrencia);
}
// GET: /Ocorrencias/Adiciona
public ActionResult Adiciona()
{
ViewBag.AlunoId = new SelectList(db.Alunos, "Id", "Nome");
return View();
}
// POST: /Ocorrencias/Adiciona
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Adiciona([Bind(Include="Id,Tipo,Causa,Observacao,AlunoId")] Ocorrencia ocorrencia)
{
if (ModelState.IsValid)
{
db.Ocorrencias.Add(ocorrencia);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.AlunoId = new SelectList(db.Alunos, "Id", "Nome", ocorrencia.AlunoId);
return View(ocorrencia);
}
// GET: /Ocorrencias/Edita/5
public ActionResult Edita(long? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Ocorrencia ocorrencia = db.Ocorrencias.Find(id);
if (ocorrencia == null)
{
return HttpNotFound();
}
ViewBag.AlunoId = new SelectList(db.Alunos, "Id", "Nome", ocorrencia.AlunoId);
return View(ocorrencia);
}
// POST: /Ocorrencias/Edita/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edita([Bind(Include="Id,Tipo,Causa,Observacao,AlunoId")] Ocorrencia ocorrencia)
{
if (ModelState.IsValid)
{
db.Entry(ocorrencia).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.AlunoId = new SelectList(db.Alunos, "Id", "Nome", ocorrencia.AlunoId);
return View(ocorrencia);
}
// GET: /Ocorrencias/Remove/5
public ActionResult Remove(long? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Ocorrencia ocorrencia = db.Ocorrencias.Find(id);
if (ocorrencia == null)
{
return HttpNotFound();
}
return View(ocorrencia);
}
// POST: /Ocorrencias/Remove/5
[HttpPost, ActionName("Remove")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(long id)
{
Ocorrencia ocorrencia = db.Ocorrencias.Find(id);
db.Ocorrencias.Remove(ocorrencia);
db.SaveChanges();
return RedirectToAction("Index");
}
And here's what I put in the Student template, which loads the student names to appear in the dropdownlist:
public ICollection<Ocorrencia> Ocorrencias { get; set; }
public Aluno()
{
this.Ocorrencias = new HashSet<Ocorrencia>();
}