ASP.NET.MVC Basic question - Register with "subform"?

2

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);
    }

And it's View:

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?

    
asked by anonymous 11.08.2017 / 14:04

1 answer

2

One method to solve this problem is:

In view within the form, creates a div that contains all allergic elements, and assign a css to control when it should appear or not, this css can be a visibility: hidden , or a display:none .

And you control click on the checkbox through a jquery . So when the checkbox is checked you make this div containing the allergy information appear.

As for the data sent from View to the Controller , clicking the submit button causes you to View pass the values that will be handled directly in Controller

EDIT:

Just as you are going to view information controller, is passing controller information to view see here . So you can take the list of allergies to view and from what I see, are using Razor, so you can make a list of checkboxes with existing allergies, see here . I think that from now on you can do it, since you already know how to pass information from view to controller. If you still have any doubts, just comment and / or modify the question.

    
11.08.2017 / 14:28