I have a cascaded dropdown list using json to populate second dropdown and also using slectpicker to search for data in the second dropdown list. The problem I have is that the second dd is not resetting on change
I use asp.net core and Razor pages
View
Code
Selecteaza sursa de pericol
Executant
Sarcina de munca
Mijloace de productie
Selecteaza pericolul
--Selecteaza Forma de manifestare--json
C#
public async Task OnGetCategorieAsync(string id)
{
var categorie = await _context.FactoriRiscs.Where(s => s.Sursa == id).Select(s => s.Categorie).Distinct().ToListAsync();
return new JsonResult(new SelectList(categorie, "Categorie"));
}
public async Task OnGetFormaManAsync(string id)
{
var forma = await _context.EvarisMains.Where(s => s.Pericol == id).Select(s => s.FormaManifestare).Distinct().ToListAsync();
return new JsonResult(new SelectList(forma, "FormaManifestare"));
}jquery
JavaScript
$(function () {
$("#Sursa").on("change", function () {
var categoryId = $("#Sursa :selected").text();
$("#Categorie").empty();
$("#Categorie").append("--Selecteaza o categorie--");
$.getJSON(`?handler=Categorie&id=${categoryId}`, (data) => {
$.each(data, function (i, item) {
$("#Categorie").append(`${item.text}`);
});
});
var data = $("#Sursa :selected").text();
$("#txtSursa").val(data);
});
});
$(function () {
$("#Categorie").on("change", function () {
var categoryId = $("#Categorie :selected").text();
$("#FormaM").empty();
$("#FormaM").append("--Selecteaza forma de manifestare sau adauga--");
$.getJSON(`?handler=FormaMan&id=${categoryId}`, (data) => {
$.each(data, function (i, item) {
$("#FormaM").append(`${item.text}`);
});
$('#FormaM').selectpicker({ liveSearch: true });
});
var data = $("#Categorie :selected").text();
$("#txtCategorie").val(data);
});
});

Sachin SinghRank #66 · 26.2K reputationPosted Jan 7, 2023, 6:25 AM EST
test this
$(function () { $("#Categorie").on("change", function () { var categoryId = $("#Categorie :selected").text(); $("#FormaM").empty(); $("#FormaM").append("--Selecteaza forma de manifestare sau adauga--"); $.getJSON(`?handler=FormaMan&id=${categoryId}`, (data) => { $.each(data, function (i, item) { $("#FormaM").append(`${item.text}`); }); $('#FormaM').selectpicker({ liveSearch: true }); $("#FormaM").selectpicker('refresh'); }); var data = $("#Categorie :selected").text(); $("#txtCategorie").val(data); }); });Marius VasileRank #785 · 2.9K reputationPosted Jan 7, 2023, 7:07 AM EST
Thank you Sachin, it worked well