I have successfully implemented a select element that can now save multiple options on the FrontEnd, however the BackEnd functionality is not yet completed.
The challenge lies in modifying the code to loop through and save all selected values, instead of just one at a time.
Unfortunately, I am unsure of how to properly implement this looping mechanism without encountering errors.
Below is a screenshot displaying the selected values along with their respective codes:
Here's the HTML code for the select element:
<div class="form-group pmd-textfield">
<label for="TipoPenalAntecedenteCriminal" class="espacamentoLabels">
Main Criminal Type
</label>
<select asp-items="Model.ListTipoPenal"
name="COD_TIPO_PENAL_PRINCIPAL"
id="TipoPenalAntecedenteCriminal"
form="formAntecedenteCriminal"
class="select-codigo" multiple
onchange="mostrarOutroDeCombo(this, 'OutroTipoPenalAntecedenteCriminal');">
<option value="">Select the relation</option>
</select>
</div>
This is the snippet of code in the Controller that needs to be modified to handle multiple selections:
This code requires modification to correctly save all specified values.
[HttpPost]
public JsonResult SalvarAntecedenteCriminal(IFormCollection form)
{
#region GET USER DATA
var identity = (ClaimsIdentity)User.Identity;
string usuarioLogado = identity.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
#endregion
...
return Json(antecendeteCriminal);
}
An error occurs while attempting to save the selected values, specifically in the line "COD_TIPO_PENAL_PRINCIPAL = int.Parse(codTipoPenal)," where the issue arises but data is not saved.