I have the next on my View:
<select id="AreaSelect" class="custom-select" name="AreaClave">
@if (ViewBag.areasSelect != null)
{
<option value="NA" selected>ALL</option>
foreach (AreasCert areasItem in (List<AreasCert>)ViewBag.areasSelect)
{
<option value="@areasItem.AreaClave">@areasItem.AreaClave</option>
}
}
</select>
And I have an Action that returns multiple ViewBags and a Model with information:
public ActionResult EmpOperaciones(CertsxOpxAreas certsxOpxAreas)
{
GetOpFromEmpleado(certsxOpxAreas);
certsxOpxAreas.EmpidCoordinador = GetOpFromEmpleado(certsxOpxAreas)[1].EmpidCoordinador;
ViewBag.areasSelect = AreaSelect();
return View(certsxOpxAreas);
}
public List<AreasCert> AreaSelect()
{
SqlConnection con = (SqlConnection)_contextFRTFGRAL.Database.GetDbConnection();
SqlCommand cmd = con.CreateCommand();
List<AreasCert> areasSelect = new();
// Realizamos una query para obtener todas las descripciones de las operaciones.
con.Open();
cmd.CommandText = "SELECT areaClave FROM cert.areasCerts " +
"WHERE Activa = 1 " +
"ORDER by areaClave ASC";
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
areasSelect.Add(new AreasCert
{
AreaClave = reader["areaClave"].ToString()
});
}
}
con.Close();
return areasSelect;
}
I want to create another select menu with options based on the selection of the user (i.e. display the operations in area1 when user selects that) My first approach was to have something like this:
function AreasSelect() {
d = document.getElementById("AreaSelect").value;
window.location.href = "?Area=" + d;
}
And have an onchange function on the select item, but I don't think this is an efficient way to achieve this, is there a way to do this as clean as posible?