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
json
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
$(function () {
$("#Sursa").on("change", function () {
var categoryId = $("#Sursa :selected").text();
$("#Categorie").empty();
$("#Categorie").append("");
$.getJSON(`?handler=Categorie&id=${categoryId}`, (data) => {
$.each(data, function (i, item) {
$("#Categorie").append(``);
});
});
var data = $("#Sursa :selected").text();
$("#txtSursa").val(data);
});
});
$(function () {
$("#Categorie").on("change", function () {
var categoryId = $("#Categorie :selected").text();
$("#FormaM").empty();
$("#FormaM").append("");
$.getJSON(`?handler=FormaMan&id=${categoryId}`, (data) => {
$.each(data, function (i, item) {
$("#FormaM").append(``);
});
$('#FormaM').selectpicker({ liveSearch: true });
});
var data = $("#Categorie :selected").text();
$("#txtCategorie").val(data);
});
});
Sachin SinghPosted Jan 7, 2023, 11:25 AM
test this
Marius VasilePosted Jan 7, 2023, 12:07 PM
Thank you Sachin, it worked well