I'm working on my first ASP.MVC project with C # and am joking with the student enrollment. On the student's registration page there is a CheckBox that when checked (I imagined) should enable the addition of allergies that the student may have. See the bank (very basic):
InaWindowsFormIwouldhaveanallergysubformassociatedwiththestudentform,sotheuserwouldselecthowmanyallergieshewantedonagridorinalistbox.Howtosolvethisinwebdevelopment?I'mconfusedbecausetheviewisassociatedwithamodel,inthecasestudent.YoursubmitbuttonwritestotheStudentstable...Thenhow,onthestudentenrollmentpage,listtheitemsintheAllergiestablefortheusertoselecttheonesyouwant.AndthenhowtoregisterintheAllergiesAlumnastablewhensubmittingtheStudentpage?
Update-01:
Inowhavethistemplate:
publicpartialclassPessoa{[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Pessoa()
{
this.AlergiasPessoas = new HashSet<AlergiasPessoas>();
}
public int ID { get; set; }
public string nome { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<AlergiasPessoas> AlergiasPessoas { get; set; }
}
This Controller:
// POST: Pessoas/Create
// 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 Create([Bind(Include = "ID,nome")] Pessoa pessoa)
{
if (ModelState.IsValid)
{
db.Pessoa.Add(pessoa);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(pessoa);
}
// GET: Pessoas/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Pessoa pessoa = db.Pessoa.Find(id);
if (pessoa == null)
{
return HttpNotFound();
}
return View(pessoa);
}
Icustomizedherwiththislistbox:
<!--ItemadicionadoparapermitirselecionarAlergias-->@{CadastroAluno.Models.TESTEEntitiesBanco=newCadastroAluno.Models.TESTEEntities();}<divclass="form-group">
<label class="control-label col-md-2" for="ID">Possui Alergia?</label>
<div class="col-md-10">
<input type="checkbox" onclick="ExibeItem(this.checked)" htmlAttributes="{ class = form-control }" />
</div>
</div>
<div class="form-group" id="Oculta" style="display:none">
@Html.LabelFor(model => model.AlergiasPessoas, "AlergiasPessoas", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.ListBoxFor(model => model.AlergiasPessoas, new SelectList(Banco.AlergiasPessoas, "ID", "ID_Alergia"),
htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.AlergiasPessoas, "", new { @class = "text-danger" })
</div>
</div>
<!-- - -->
and with this java:
<script type="text/javascript">
function ExibeItem(CheckBox) {
if (CheckBox == false) {
document.getElementById('Oculta').style.display = 'none';
} else {
document.getElementById('Oculta').style.display = 'block';
}
}
but the ListBox is still empty ... How to fill it with the items registered in the Allergy table?