I have created a view where when the user selects 2 of the dropdown lists values, an AJAX method is kicked off to populate an additional dropdown list's values
The callback triggers, however I cannot work with the response.
I am probably doing more than one thing wrong, so any help would be greatly appreciated. I am very new to Asp.Net core mvc
My Controller code (simplified - parameters are being used and are working):
public IActionResult GetCodes(string location, string xType)
{
return Json(new Dictionary<string, string> {
{ "","" },
{ "Option1","120" },
{ "Option2","123" }
});
}
My jquery script on the view:
var ddl1val = $("#Location :selected").val().toLowerCase();
var ddl2val = $("#xType:selected").val().toLowerCase();
$.ajax({
type: "GET",
url: "/Ppl/GetCodes",
dataType: 'json',
data: { location: ddl1val, xtype: ddl2val},
success: function (data) {
$("#ddlOptions").empty();
var ops = '<option value=""></option>';
alert(data); //returns [object Object]
alert(data.Key); //returns undefined???
//I need this to work
for (var i = 0; i < data.length; i++) {
ops += '<option value="' + data[i].Key + '">' + data[i].Value + '</option>';
$("#ddlOptions").html(ops);
}
}
console.log(data) shows:
{"": "", Option1: "120", Option2: "123"}
I am not restricted to use JSON return, but it is lighter than xml, so I would prefer using it