Hello,
I need in EDIT page to recalculate at runtime a lookup table field (x_IDListinoTariffa) refilling it with options from a SQL SELECT based on four other fields, every time the user changes one of the four fields x_IDStruttura,x_IDSezione,x_IDAnno,x_IDFasciaOraria.
If the user changes one of the four fields, the lookup table options in x_IDListinoTariffa must be recalculate and the listbox repopulated.
I started from the “Ajax by API and Client Scripts” example in Lookup guide item.
So I have in Api_Action the following code:
public class GetDomandeIDListinoTariffaController : ApiController {
[HttpGet("{pidstruttura}/{pidsezione}/{pidanno}/{pidfasciaoraria}")]
public IActionResult Get([FromRoute] string pidstruttura, string pidsezione, string pidanno, string pidfasciaoraria) {
string sSQL=$"SELECT IDListino, Denominazione, IDStruttura, IDSezione, IDAnno, IDFasciaOraria FROM listini WHERE CodTipo='R' AND IDStruttura='{pidstruttura}' AND (IDAnno IS NULL OR IDAnno='{pidanno}') AND (IDSezione IS NULL OR IDSezione='{pidsezione} AND (IDFasciaOraria IS NULL OR IDFasciaOraria='{pidfasciaoraria}')";
var res = ExecuteRows(sSQL);
return Json(res);
}
}
And in Client scripts/Table-Specific/Edit Page/Startup Script the following code:
$("#x_IDSezione").on("change", function (e) {
$.get(ew.getApiUrl("GetDomandeIDListinoTariffa/" + $("#x_IDStruttura").val() + "/" + $("#x_IDSezione").val() + "/" + $("#x_IDAnno").val() + "/" + $("#x_IDFasciaOraria").val()), function(res) {
if (res) {
console.log(res);
$('#x_IDListinoTariffa').select2(res).trigger('change');
}
});
});
The code works ok: every time I change the x_IDSezione field the api method is called and the correct records retrieved.
My problem is that the destination field x_IDListinoTariffa is not updated: it looses its “option template” formatting but the options remain the same.
What’s wrong? How can I fill a SELECT2 lookup table destination field in the correct way, WITHOUT loosing its “option template” formatting?
I’m using AspNetMaker 2021 in this project but it should be the same.
Any idea?